Incase anyone is looking for how I did this (I've seen many posts asking about this), here's the code.
First off, I'm doing this in a TableViewController:
In my H file:
Code:
@interface AddItemView : UITableViewController <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UIActionSheetDelegate>{
UIPickerView *pickerView;
UIToolbar *pickerToolbar;
UIActionSheet *pickerViewPopup;
}
- (void) showPicker;
In my M file:
This triggers the actionsheet to appear when the user enters the specified UITextField:
Code:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField == newItem.quantityF)
{
[textField resignFirstResponder];
[self showPicker];
[pickerView selectRow:1 inComponent:3 animated:YES];
}
}
This shows and hides the actionsheet containing the pickerView:
Code:
- (void) showPicker {
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:@"How many?"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,44,0,0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES; // note this is default to NO
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closePicker:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
-(BOOL)closePicker:(id)sender
{
[pickerViewPopup dismissWithClickedButtonIndex:0 animated:YES];
[pickerView release];
[pickerToolbar release];
[pickerViewPopup release];
return YES;
}
Here's the pickerView setup: (In the M file)
Code:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSArray *rowTitles = [NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil];
return [rowTitles objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 4;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 10;
}
-(void)pickerView:(UIPickerView *)aPickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}