How to popup a UIPickerView from the bottom of a UIScrollView in response to UITextField selection

转载: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

presentModalViewController : (UIViewController  * )modalViewController animated : ( BOOL )animated

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 UIActionSheet, add your UIPickerView to it, set the callback delegate (to get which button is clicked) and your away!

Code…

-  ( NSString  * )pickerView : (UIPickerView  * )pickerView titleForRow : (NSInteger )row forComponent : (NSInteger )component {
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.)

-  ( void ) showPicker : ( id )sender  {
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..

-  ( void )actionSheet : (UIActionSheet  * )actionSheet clickedButtonAtIndex : (NSInteger )buttonIndex {
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 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.

regionData  =  [ [ NSArray arrayWithObjects :
@ "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

@interface myViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UIActionSheetDelegate> {

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.

-  ( void )pickerView : (UIPickerView  * )pickerView didSelectRow : (NSInteger )row inComponent : (NSInteger )component {
selectedScrollIndex  = row;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值