Ios 按Donebutton关闭UIPickerView
我有这个密码。文本字段的UIPickerView。Ios 按Donebutton关闭UIPickerView,ios,objective-c,uipickerview,Ios,Objective C,Uipickerview,我有这个密码。文本字段的UIPickerView。 我想这样设计 当用户编辑UITextField时,将显示UIPickerView和donebutton, UIPickerView通过按donebutton关闭 问题是未显示doneButton 因此,选择器无法关闭 ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegat
我想这样设计
当用户编辑
UITextField
时,将显示UIPickerView
和donebutton
,UIPickerView
通过按donebutton
关闭问题是未显示
doneButton
因此,选择器无法关闭 ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@end
您知道如何修复它吗?您也可以通过设置
UITextField
对象的inputView
来实现:
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
textField.inputView = _pickerView;
textField.inputAccessoryView = self.accessoryView_;
return YES;
}
其中属性accessoryView_uu可以用以下内容实例化:
self.accessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, _pickerView.frame.size.width, 40)];
[(UIToolbar *) self.accessoryView_ setItems:@[
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissPicker)]
]];
代码中的某个地方(viewDidLoad将是一个很好的地方)
解雇拾荒者很简单
- (void) dismissPicker
{
[_textField resignFirstResponder];
}
当然,您必须使用属性或ivar保留对
文本字段的引用
,如果我理解正确,您就无法在导航栏(右侧)上显示“完成”按钮。如果当前的UIViewController
不是UINavigationController
层次结构的一部分,或者未将UINavigationController
初始化为rootViewController,则导航项将无法正常工作
您可以通过下面的代码段确认这一点,如果它起作用,则视图控制器是UINavigationController
的一部分,如果不起作用,则应检查您的UINavigation层次结构
self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"done", @"Done")
style:UIBarButtonItemStyleDone
target:self action:@selector(doneAction)];
请参阅“完成”按钮的位置、选择器视图后面或“您的导航栏显示在哪里?”谢谢您的回答。这是非常有用的。
self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"done", @"Done")
style:UIBarButtonItemStyleDone
target:self action:@selector(doneAction)];