@interface ViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>{ UIToolbar *tool;//主要用这存放按钮 } @property (retain, nonatomic) IBOutlet UIDatePicker *picker; @property (retain, nonatomic) IBOutlet UITextField *text; @property (retain, nonatomic) IBOutlet UITextField *textField; @end
tool;
控件,
- (void)viewDidLoad { [super viewDidLoad]; tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30.0f)]; tool.barStyle = UIBarStyleBlackTranslucent;//实现Uitoolbar,他的位置不重要,主要是大小, //toolbar上面放得就是items控件,因为是在左右两边都放一个,中间没有,中间放了2个空的可以达到效果,因为自己不能实现item自动位置放置, UIBarButtonItem *previousBarItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(previousField:)]; //空的itme占空位 UIBarButtonItem *nextBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *spaceBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"完成", @"") style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard:)]; //添加到tool上面 [tool setItems:[NSArray arrayWithObjects:previousBarItem,nextBarItem,spaceBarItem,doneBarItem,nil]]; _text.inputView=_picker;//这块设置比较重要,textfiled的inputview是picker, _text.delegate=self; _text.inputAccessoryView=tool;//textfiled 的inputAccessoryview的是tool,原因我也具体不是不说了,看别人的, _textField=[[UITextField alloc]init];//这个临时的textfiled主要是实现picker的弹回去,就是隐藏, } -(void)textFieldDidBeginEditing:(UITextField *)textField{ _textField=textField;//把textfiled控件赋予给临时的textfiled控件 } -(void)resignKeyboard:(id)sender{ //实现picker隐藏,实现方法和键盘弹起收回一样,具体原因也不是很明白, [_textField resignFirstResponder]; }上面就是简单的实现tool的添加,