Ios 在@selector方法中传递参数
在Ios 在@selector方法中传递参数,ios,objective-c,arguments,selector,Ios,Objective C,Arguments,Selector,在@selector方法中传递参数时出错 这是我的代码: -(void) accessoryView : (UITextField*) textField { UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; numberToolbar.barStyle = UIBarStyleBlackTranslucent; numberToolbar.ite
@selector
方法中传递参数时出错
这是我的代码:
-(void) accessoryView : (UITextField*) textField
{
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = @[[[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(doneWithNumberPad:textField:)]];
[numberToolbar sizeToFit];
textField.inputAccessoryView = numberToolbar;
}
-(void)doneWithNumberPad : (UITextField*) txt : (id) sender {
}
第一:你的选择器错了。我应该是
操作:@selector(doneWithNumberPad::)
或者将第二个方法重命名为-(void)doneWithNumberPad:(UITextField*)txt textField:(id)sender
但是第二点:您不能使用操作方法发送自定义的附加参数。您应该访问属性上的文本字段。在您的情况下,可以在开始编辑文本字段时将当前文本字段存储在属性中。视图控制器应实现
UITextFieldDelegate
以接收此事件。首先:选择器错误。我应该是操作:@selector(doneWithNumberPad::)
或者将第二个方法重命名为-(void)doneWithNumberPad:(UITextField*)txt textField:(id)sender
但是第二点:您不能使用操作方法发送自定义的附加参数。您应该访问属性上的文本字段。在您的情况下,可以在开始编辑文本字段时将当前文本字段存储在属性中。视图控制器应实现
UITextFieldDelegate
以接收此事件。您可以使用文本字段的参考变量。将全局属性声明为textfield。保留当前活动文本字段的引用,并根据您的要求在每个方法中访问它
UITextField *activeTextField;
// UITextField Delegates
-(void)textFieldDidBeginEditing:(UITextField *)textField {
activeTextField = textField;
}
您可以在类的任何位置使用此activeTextField属性
您需要将类定义为UITextField的委托
self.yourTextField.delegate = self
您可以为您的文本字段使用参考变量。将全局属性声明为textfield。保留当前活动文本字段的引用,并根据您的要求在每个方法中访问它
UITextField *activeTextField;
// UITextField Delegates
-(void)textFieldDidBeginEditing:(UITextField *)textField {
activeTextField = textField;
}
您可以在类的任何位置使用此activeTextField属性
您需要将类定义为UITextField的委托
self.yourTextField.delegate = self
不能将任意参数传递给操作选择器;选择器必须符合条形按钮项所期望的格式是否有其他方法将“textField”传递给“doneWithNumberPad”方法?不能将任意参数传递给操作选择器;选择器必须符合条形按钮项所期望的格式是否有其他方法将“textField”传递给“doneWithNumberPad”方法?我总共有10个textField,其中5个需要调用accessoryView。我如何在这个场景中使用上面的代码。我已经扩展了我的帖子。谢谢你的回答。我总共有10个文本字段,其中5个需要调用accessoryView。我如何在这个场景中使用上面的代码。我已经扩展了我的帖子。谢谢你的回答谢谢你的回答谢谢你的回答