转载:http://www.wetware.co.nz/blog/2009/02/how-to-popup-a-uipickerview-from-the-bottom-of-a-uiscrollview-in-response-to-uitextfield-selection/
It says to do this with
For the life of me, that wouldn’t work. I could get it to popup sometimes, but with a horrible messed up UIPicker. I added the UIPickerView to another UIView, which was put into a UIViewController, and handed that to the above method.
This worked brilliantly, BUT after animating from the bottom the background would go completely white. Not good.
Welcome UIActionSheet
It is not available via the Interface Builder, which is where I endeavor to do all my UI (against the recommendations of everyone in the office.)
Basically, you create a UIActionSheet, add your UIPickerView to it, set the callback delegate (to get which button is clicked) and your away!
Code…
return ( NSString * ) [currentData objectAtIndex :row +1 ];
}
- (NSInteger )numberOfComponentsInPickerView : (UIPickerView * )pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [currentData count] - 1;
}
Put that into your controller (and smoke it.)
UIActionSheet *menu = [ [UIActionSheet alloc ] initWithTitle : [currentData objectAtIndex : 0 ]
delegate :self
cancelButtonTitle : @ "Done"
destructiveButtonTitle : @ "Cancel"
otherButtonTitles : nil ];
// Add the picker
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,185,0,0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES; // note this is default to NO
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 700)];
[pickerView release];
[menu release];
}
It’s not pretty, but it works. Again put that into your controller.
Respond to UIActionSheet buttons..
if (buttonIndex ==1 ) {
if (currentField == location ) {
if ( !completedOnce ) {
[website becomeFirstResponder ];
}
} else {
completedOnce = YES;
}
}
}
Again, in your controller. (I left in my actual code, to give an idea..)
What mine basically does is as follows
- Setup a couple of arrays with data (content for UIPickerView) (for reference different UITextFields)
- When a UITextField gets textFieldShouldBeginEditing I return NO and instead callshowPicker.
- Before showPicker is called, I set an instance variable to the array the UITextField relates to. This sets it up for the UIPickerView to use.
- showPicker shows the ActionSheet, with the data it needs.
- showPicker returns, calling the clickedButtonAtIndex
- Then I decide what to do with it
There are some minor details I have left out, but you should be able to fill in the gaps.
[Edit]
If you are wondering why I return the currentData objectAtIndex:row+1 it is because my arrays have the first Item the title of the UIPickerView.
@ "Select Your Region",
@ "Waikato",
@ "Hawkes Bay",
@ "Marlborough",
@ "Taranaki",
@ "Gisborne",
@ "Bay of Plenty",
@ "Nelson",
@ "Otago",
@ "Southland",
@ "Northland",
@ "Manawatu-Wanganui",
@ "West Coast",
@ "Canterbury",
@ "Wellington",
@ "Auckland",
@ "Tasman", nil ] retain ];
Don’t forget to add
to your .h file.
[EDIT]
To get the selectedIndex from the UIPicker, you need to add the following to your controller. This is in response to a comment below. I haven’t been deving the iPhone recently so I only guessed the below solution is correct (cherry picked it out of my code). If not please point it out and I’ll go back and work it out again.
selectedScrollIndex = row;
}