1.xib上的名為valueTextField的文本輸入框的屬性改為如下情形
2.插座變量
@property (weak, nonatomic) IBOutlet UITextField *valueTextField;
3.取消第一響應者
-(void)numberFieldCancle{ [self.valueTextField resignFirstResponder]; }
3.生成ToolBar
- (UIToolbar *)addToolbar { UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 50)]; // UIToolbar *toolbar =[[UIToolbar alloc] init]; UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(numberFieldCancle)]; toolbar.items = @[bar]; return toolbar; }
4.鍵盤升起時給鍵盤添加按鈕
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if (textField==self.valueTextField) { self.valueTextField.inputAccessoryView=[self addToolbar]; } return YES; }
5.注意哦,這個.m要遵守UITextFieldDelegate協議
@interface BNRItemDetailViewController ()<UITextFieldDelegate>