问题
I'm working on a nib file that has multiple UIPickerViews on it and as you can see below, it is extremely crowded. I basically want to make it clean and remove all of the "gray" items and just show the selected item. If you have any ideas that would make the page cleaner please share, this is more of a UI problem than a logic or code problem. Thanks in advance.
回答1:
You dont need to add UIPickerView in there. You just add text field and set your UIPickerView as text field's input accessory view. So your picker will appear in the bottom side and you can display the selected values of picker in the text field by using delegate methods.
回答2:
Make sure you add your <UIActionSheetDelegate> to your .h file
I used this code for a datepicker with a button next to a text field. Any time you clicked on this button, this would pop up in an action sheet over it. You might have to tweak this a little:
In your button event add this code:
UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:@"Pick the Category" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Set Category", nil];
[asheet showInView:[self.view superview]]; //note: in most cases this would be just self.view, but because I was doing this in a tabBar Application, I use the superview.
[asheet setFrame:CGRectMake(0, 117, 320, 383)];
tagOfDateToUse = 1; //Pick any number you want to use as a tag.
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
//Configure picker...
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
//Configure picker...
//Add picker to action sheet
[asheet addSubview:pickerView];
Then add this method:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// Here is where you will put an IF check to see what tag it is and within that if
if (tagOfDateToUse == 1)
{
UIDatePicker *ourDatePicker;
ourDatePicker = [[UIDatePicker alloc] init];
NSArray *subviews = [actionSheet subviews];
ourDatePicker = [subviews objectAtIndex:4];
NSDate *selectedDate = [ourDatePicker date];
}
}
And finally:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 255, 280, 46)];
[[subviews objectAtIndex:3] setFrame:CGRectMake(20, 305, 280, 46)];
};
NOTE: You may need to change the subview ObjectAtIndex numbers based on what your set and cancel buttons are.
来源:https://stackoverflow.com/questions/20137284/removing-non-selected-items-from-uipickerview