简体   繁体   English

填充所有 UITextField 后,UIButton 未启用

[英]UIButton is not enabling when all UITextFields are filled in

I am working on an iOS application where a button is enabled only after all UITextField s that are filled in (ie have at least one character inside each one).我正在开发一个 iOS 应用程序,其中只有在填充了所有UITextField之后才启用按钮(即每个里面至少有一个字符)。 Each UITextField is in a custom UITableViewCell inside of a UITableView .每个UITextField都位于UITableViewCell内的自定义UITableView Once the user has entered a value in each UITextField , I need the button to be enabled.一旦用户在每个UITextField输入了一个值,我就需要启用该按钮。 Inside my viewDidLoad method, I disable the button as follows:在我的viewDidLoad方法中,我禁用按钮如下:

[_doneButton setEnabled:NO];

and then I have the following method:然后我有以下方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    for (int i = 0; i < [self.table numberOfSections]; i++) {

        NSInteger rows =  [self.table numberOfRowsInSection:i];

        for (int row = 0; row < rows; row++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:i];
            SimpleTableCell *cell = (SimpleTableCell *)[self.table cellForRowAtIndexPath:indexPath];

            for (UIView *subView in cell.subviews) {

                if ([subView isKindOfClass:[UITextField class]]) {
                    //Does not come to this point
                    UITextField *txtField = (UITextField *)subView;

                    if([[txtField text] length] > 0) {

                        //Enable button and bold title
                        [_doneButton setEnabled:YES];
                        [_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:28]];

                    }

                     else {

                         [_doneButton setEnabled:NO];

                     }

                }

            }

        }

    }

    return YES;
}

Unfortunately, my code does not get into the "if" clause where I check to see if the custom cell has a subView of type UITextField , and because of that, the button is never enabled.不幸的是,我的代码没有进入“if”子句,在那里我检查自定义单元格是否具有UITextField类型的子UITextField ,因此,该按钮从未启用。 For the record, "SimpleTableCell" is the name of my custom UITableViewCell class that I've created.作为记录,“SimpleTableCell”是我创建的自定义UITableViewCell类的名称。 My code appears to be correct, but I can't figure out why it is not functioning correctly.我的代码似乎是正确的,但我不知道为什么它不能正常运行。 Can anyone see what it is I'm doing wrong?谁能看到我做错了什么?

That's because the only logical subview of the cell is an UIView containing all the items of the cell.这是因为单元格的唯一逻辑子视图是包含单元格所有项目的 UIView。 It's property is "contentView".它的属性是“contentView”。

The content view of a UITableViewCell object is the default superview for content displayed by the cell. UITableViewCell 对象的内容视图是单元格显示内容的默认超级视图。 If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.如果您想通过简单地添加其他视图来自定义单元格,您应该将它们添加到内容视图中,以便在单元格进入和退出编辑模式时将它们适当地定位。

More information about it here: https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/contentView关于它的更多信息: https : //developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/contentView

I think I would go about this in a completely different way.我想我会以完全不同的方式来解决这个问题。 I would create a mutable dictionary to hold the strings in the various text fields.我会创建一个可变字典来保存各种文本字段中的字符串。 Give the text fields tags, and use the tags (converted to NSNumbers) as the keys in the dictionary.给文本字段标签,并使用标签(转换为 NSNumbers)作为字典中的键。 Implement textFieldDidEndEditing:, and in that method get the string, check that it's not an empty string, and if not, do setObject:forKey: on the dictionary.实现 textFieldDidEndEditing:,并在该方法中获取字符串,检查它是否不是空字符串,如果不是,则在字典上执行 setObject:forKey: 。 After you set the value, check the count of the dictionary's allKeys property, and if it equals the number of text fields you have, enable the button.设置值后,检查字典的 allKeys 属性的计数,如果它等于您拥有的文本字段数,则启用该按钮。

Have you set the UITextField's delegate?您是否设置了 UITextField 的委托? If that method is not getting called at all, that is the problem.如果根本没有调用该方法,那就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM