gpt4 book ai didi

ios - 在多个 UITextField 的数字键盘上显示完成按钮

转载 作者:行者123 更新时间:2023-12-01 16:39:39 25 4
gpt4 key购买 nike

@Luda's answer is a great answer ,但是当我需要将它用于多个文本字段时我被卡住了,所以我将其编辑如下:

首先,我为每个文本字段获取 IBOutlets,例如:textField1、textField2

然后我将代码编辑为

- (void)viewDidLoad
{
[super viewDidLoad];

UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad:)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(sendToServer:)],
nil];
[numberToolbar sizeToFit];
textField1.inputAccessoryView = numberToolbar;
textField2.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad:(UITextField *)textField
{
//here I use if/else to determine which textField was tapped
if(textField == self.textField1)
{
//do some stuff
}else //...

}

-(void) sendToServer:(UITextField *)textField
{
//here I use if/else to determine which textField was tapped
if(textField == self.textField1)
{
//do some stuff
}else //...
}

注意我必须添加冒号 :@selector例如 @selector(sendToServer:)这样,正确的 TextField 作为参数传递。

但是

它不工作。测试失败: if(textField == self.textField1) .那么有谁知道如何正确地做到这一点?

问题是:我如何知道正在编辑哪个文本字段?

最佳答案

您支票的原因if (textField == self.textField)失败是因为默认传递给 UIBarButtonItem 选择器的参数实际上是 UIBarButtonItem 本身。您可以通过在任一选择器方法中放置断点并检查 textField 参数来验证这一点。

一种可能的解决方案是,在不引入任何代码来循环 subview 的情况下,将您的 View Controller 声明为 UITextFieldDelegate,然后按如下方式修改您的选择器:

- (void)sendToServer:(UIBarButtonItem*)barButtonItem {
[self.view endEditing:NO];
}

然后实现委托(delegate)方法textFieldShouldEndEditing:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
// here you would determine which text field the UIBarButtonItem was associated with
if(textField == self.textField1)
{
// your code here
// and then, if you wanted to dismiss the keyboard
return YES;
} else {
return NO;
}
}

关于ios - 在多个 UITextField 的数字键盘上显示完成按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25492719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com