我有二合UITextfield
二合一其中叫和,它们设置为 0 和 1。我在 的右边缘有一个叫。我希望在此视图进入屏幕时禁用该按钮,并希望在两者都在其中输入文本时启用该按钮,否则该按钮被禁用。cells
section
grouped UITableView
emailField
passwordField
tag
UIBarButtonItem
loginButton
navigationBar
textFields
这是创建按钮的代码。
_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;
}
}
这有什么问题?