处理两个UITextField和委托

时间:2013-07-26 08:02:21

标签: objective-c

嗨,我很容易照顾一个UITextField。例如,设置UITextField的委托我的视图控制器并实现这样的方法:

- (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;
    }

3 个答案:

答案 0 :(得分:4)

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

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

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


    return YES;
}

答案 1 :(得分:1)

您可以使用标记更具体地针对每个文本字段委托方法:

if (textField.tag == 1) {
        UITextField *passwordTextField = (UITextField *)[self.view viewWithTag:2];
        [passwordTextField becomeFirstResponder];
    }
    else {
        [textField resignFirstResponder];
    }

请查看此参考了解更多信息。 http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uitextfield-uitextfielddelegate/

答案 2 :(得分:1)

就只是辞职第一响应者而言,这样做:

- (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(或其中任何一个)对象的任何其他合适的引用,则可以使用UIView tag属性来识别它们,区分他们。