0

我有二合UITextfield二合一其中叫和,它们设置为 0 和 1。我在 的右边缘有一个叫。我希望在此视图进入屏幕时禁用该按钮,并希望在两者都在其中输入文本时启用该按钮,否则该按钮被禁用。cellssectiongrouped UITableViewemailFieldpasswordFieldtagUIBarButtonItemloginButtonnavigationBartextFields

这是创建按钮的代码。

_loginButton = [[UIBarButtonItem alloc] initWithTitle:@"Login"
                                                   style:UIBarButtonItemStyleDone
                                                   target:self
                                                   action:@selector(logging)];

    _loginButton.enabled = NO;
    self.navigationItem.rightBarButtonItem = _loginButton;

UITextField textFieldDidBegingEditing:方法中,我试图实现这个功能。但它不起作用或工作不正确,这很奇怪。注意:textFields添加为cell'ssubview而不是cell contentView's subview.

#pragma mark - UITextfield Delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.emailField)
    {
        UITableViewCell *currentCell = (UITableViewCell *)textField.superview;
        NSIndexPath *currentIndexPath = [self.tableView indexPathForCell:currentCell];
        NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row+1 inSection:0];
        UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:nextIndexPath];
        self.passwordField = (UITextField *)[nextCell viewWithTag:1];
        if ([(UITextField *)[currentCell viewWithTag:0] text].length != 0 && [(UITextField *)[nextCell viewWithTag:1] text].length != 0)
        {
            self.loginButton.enabled = YES;
        }
        else
        {
            self.loginButton.enabled = NO;
        }
    }
}

我也尝试在按钮选择器方法中实现此功能,但它根本不起作用。

- (IBAction)logging
{
    if (self.emailField.text.length > 0 && self.passwordField.text.length > 0)
    {
        self.loginButton.enabled = YES;
    }
    else
    {
        self.loginButton.enabled = NO;
    }
}

这有什么问题?

4

1 回答 1

0

您是否将文本字段委托属性设置为自我?这段代码还有其他一些问题,但我会先检查一下

于 2013-07-03T13:18:26.660 回答