我有一个带有自定义数字键盘的应用程序。我需要一些自定义数字键盘,但不是首选项 View 中的所有文本字段。我写了这个方法来为某些文本字段设置数字键盘:
-(void)setNumberPadFor:(UITextField*)textField andNibName:(NSString *)nibNamed{
textField.inputView = [[[NSBundle mainBundle] loadNibNamed:nibNamed
owner:self
options:nil] lastObject];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone
target:textField
action:@selector(resignFirstResponder)];
UIToolbar *tips = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
tips.barStyle = UIBarStyleBlackOpaque;
[tips setBackgroundColor:[UIColor blueColor]];
[tips setTintColor:[UIColor whiteColor]];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[tips sizeToFit];
tips.items = [NSArray arrayWithObjects:spacer, btnDone, nil];
textField.inputAccessoryView = tips;
}
我按如下方式调用设置方法(在 ViewController 的 viewDidLoad 中):
[self setNumberPadFor:txtMinFeet andNibName:@"PosNegNumberPad"];
[self setNumberPadFor:txtMaxFeet andNibName:@"PosNegNumberPad"];
那么,在我的按钮点击事件中,我如何访问启动数字键盘的文本字段?
-(IBAction)btnNum1:(id)sender{
[[UIDevice currentDevice] playInputClick];
// update text field
}
-(IBAction)btnNum2:(id)sender{
[[UIDevice currentDevice] playInputClick];
// update text field
}
最终我将有两个自定义数字键盘来满足要求,但现在只有一个(因此当前将一个 Nib 作为参数传递)。
我的按钮操作已设置并正常工作,也就是说,我可以在其中设置断点并验证应用程序是否响应事件。
我不清楚的是如何识别哪个文本字段启动了数字键盘,以便我可以相应地更新它。
显然我漏掉了一些东西。有什么建议么?谢谢!
最佳答案
一种方法是使用 ivar,每次获得委托(delegate)方法 textFieldDidBeginEditing: 时都设置 ivar。稍后,您查询 ivar 以获取当前文本字段。
另一种方法是找到第一响应者。这意味着遍历主视图的 subview 以找到它。这很常见,您肯定可以在此处找到执行此操作的代码。另一种方法最容易做到。
关于ios - iOS 应用程序中自定义数字键盘的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20292821/