[英]Need to enable a UIButton only when multiple UITextFields are filled in iOS
我正在開發一個 iOS 應用程序,其中我有一個包含自定義 UITableViewCell 的 UITableView。 表格中的每個單元格都包含一個接收數字輸入的 UITextField。 在 UITableView 下方是另一個包含按鈕的視圖,需要禁用其中一個按鈕,直到 UITableView 中的所有 UITextField 都已填充。 一旦所有的 UITextFields 都已滿,那么按鈕才會被啟用。 我該怎么做呢? 我有以下相關代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
static NSString *cellIdentifier = @"Cell";
_cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (_cell == nil) {
_cell = [[SimpleTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
_cell.dataField.tag = indexPath.row;
_cell.dataField.delegate = self;
_cell.dataField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[_cell.dataField setTextAlignment:NSTextAlignmentRight];
[_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return _cell;
}
我正在實施UITextFieldDelegate
並且我認為我的解決方案必須使用該方法:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
}
但是,我不確定如何僅在表中的所有 UITextField 都包含數據時啟用特定按鈕。 我怎樣才能做到這一點?
我在我的代碼中添加了以下方法:
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
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]]) {
//NEVER REACHES 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]];
}
}
}
}
}
return YES;
}
在調用此方法時,問題在於它永遠不會到達 if 語句內部:
if ([subView isKindOfClass:[UITextField class]]) {...}
即使當我在這里放置斷點時,我在變量 subViewsCache 中看到其中一個子視圖確實是 UITextField 類型。 我在 viewDidLoad 方法中將按鈕的 enabled 屬性設置為“NO”,但不幸的是,它仍然保持這種狀態。 我究竟做錯了什么?
可能是這個 Bellow Method 你得到了 UITableview 的所有文本文件並將你的邏輯設置為標准你需要將 Bellow Method 放入textFieldShouldEndEditing
希望這對你有幫助
for (int i = 0; i < [self.tableView numberOfSections]; i++)
{
NSInteger rows = [self.tableView numberOfRowsInSection:i];
for (int row = 0; row < rows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:i];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
for (UIView *subView in cell.subviews)
{
if ([subView isKindOfClass:[UITextField class]])
{
UITextField *txtField = (UITextField *)subView;
if(txtField.text.length>0)
{
//found value in textfiled
}
else
{
//Emplty textfiled
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.