Objective c 处理两个UITextField和委托

Objective c 处理两个UITextField和委托,objective-c,Objective C,嗨,我很容易就搞定了一个UITextField。e、 g.设置UITextField my view控制器的委托,并实现该方法: - (BOOL)textFieldShouldReturn:(UITextField *)textField { // Removes the keyboard from the screen [self.textFieldProperty1 resignFirstResponder]; return YES; } 但是如果我有两个UI

嗨,我很容易就搞定了一个UITextField。e、 g.设置UITextField my view控制器的委托,并实现该方法:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

    // Removes the keyboard from the screen
    [self.textFieldProperty1 resignFirstResponder];


    return YES;
}
但是如果我有两个UITextField呢?这两个视图的代理仍将是我的视图控制器。那么我如何实现上述方法呢?像这样

 - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {

        // Removes the keyboard from the screen
        [self.textFieldProperty1 resignFirstResponder];
        [self.textFieldProperty2 resignFirstResponder];


        return YES;
    }

在将委托传递给属性时更改方法,它将自动标识从哪个textField方法调用:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

    // Removes the keyboard from the screen
    [textField resignFirstResponder];


    return YES;
}

您可以对每个textfield deleget方法使用更具体的标记:

if (textField.tag == 1) {
        UITextField *passwordTextField = (UITextField *)[self.view viewWithTag:2];
        [passwordTextField becomeFirstResponder];
    }
    else {
        [textField resignFirstResponder];
    }
请查看此参考以了解更多信息。

就辞职第一响应者而言,这样做可以:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
}
如果要添加更多后期处理,请执行以下操作:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    if (textField == self.textFieldProperty1) {
        //process property 1 here
    } else {
        // process property 2 here 
    }
}

如果您没有持有对
UITextField
(或whicheveer)对象的属性或任何其他适当引用,则可以使用
UIView
标记
属性来识别它们并区分它们

事实上,你就是这样做的。