首页 > 代码库 > 简单的实现UIpicker上面的取消确定按钮

简单的实现UIpicker上面的取消确定按钮

1 因为我用的xib实现的添加picker 和textfiled的,

  1. @interface ViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>{  
  2.     UIToolbar *tool;//主要用这存放按钮  
  3. }  
  4. @property (retain, nonatomic) IBOutlet UIDatePicker *picker;  
  5. @property (retain, nonatomic) IBOutlet UITextField *text;  
  6. @property (retain, nonatomic) IBOutlet UITextField *textField;  
  7. @end  


tool;

控件,
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.      
  5.         tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30.0f)];  
  6.         tool.barStyle = UIBarStyleBlackTranslucent;//实现Uitoolbar,他的位置不重要,主要是大小,  
  7.           
  8.           
  9.           
  10.         //toolbar上面放得就是items控件,因为是在左右两边都放一个,中间没有,中间放了2个空的可以达到效果,因为自己不能实现item自动位置放置,  
  11.         UIBarButtonItem *previousBarItem = [[UIBarButtonItem alloc] initWithTitle:@"取消"  style:UIBarButtonItemStyleBordered  
  12.                                                                            target:self  
  13.                                                                            action:@selector(previousField:)];  
  14.         //空的itme占空位  
  15.         UIBarButtonItem *nextBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  
  16.                                                                                      target:nil  
  17.                                                                                      action:nil];  
  18.         UIBarButtonItem *spaceBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  
  19.                                                                                       target:nil  
  20.                                                                                       action:nil];  
  21.         UIBarButtonItem *doneBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"完成", @"")  
  22.                                                                         style:UIBarButtonItemStyleDone  
  23.                                                                        target:self  
  24.                                                                        action:@selector(resignKeyboard:)];  
  25.           
  26. <span style="white-space:pre">  </span>//添加到tool上面  
  27.         [tool setItems:[NSArray arrayWithObjects:previousBarItem,nextBarItem,spaceBarItem,doneBarItem,nil]];  
  28.     
  29.     _text.inputView=_picker;//这块设置比较重要,textfiled的inputview是picker,  
  30.     _text.delegate=self;  
  31.     _text.inputAccessoryView=tool;//textfiled 的inputAccessoryview的是tool,原因我也具体不是不说了,看别人的,  
  32.       
  33.     _textField=[[UITextField alloc]init];//这个临时的textfiled主要是实现picker的弹回去,就是隐藏,  
  34. }  
  35.   
  36. -(void)textFieldDidBeginEditing:(UITextField *)textField{  
  37.     _textField=textField;//把textfiled控件赋予给临时的textfiled控件  
  38. }  
  39. -(void)resignKeyboard:(id)sender{  
  40.     //实现picker隐藏,实现方法和键盘弹起收回一样,具体原因也不是很明白,  
  41.     [_textField resignFirstResponder];<span style="color:#ffffff;">  
  42. }</span>  
上面就是简单的实现tool的添加,

简单的实现UIpicker上面的取消确定按钮