- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个像这样动态生成 UITextField 的代码:
UITextField *myTextfield;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 640.0f, 0.0f)];
//for (int i=0; i<4; i++) {
myTextfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 60*i+20, 280, 45)];
myTextfield.text = [NSString stringWithFormat:@"Value: %d", section];
myTextfield.enabled = NO;
myTextField.tag = section;
[headerView addSubview:myTextField];
}
我还在每个文本字段旁边生成了一个编辑按钮。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"cell1";
cell = (ButtonCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ButtonCell"
owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[ButtonCell class]])
cell = (ButtonCell *)oneObject;
}
cell.msg_btn.tag = indexPath.section;
cell.changecolor_btn.tag = indexPath.section;
cell.picklist_btn.tag = indexPath.section;
cell.adddelsubmenu_btn.tag = indexPath.section;
cell.done_btn.tag = indexPath.section;
[cell.msg_btn addTarget:self action:@selector(editEachTextField:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
现在我想在单击其中一个按钮时启用和访问每个文本字段。我正在使用 tag 来访问这些文本字段,如下所示:
- (void) editEachTextField:(UIButton*)sender
{
myTextfield = (UITextField *)[self.view viewWithTag:myTextfield.tag];
myTextfield.enabled = YES;
[myTextfield becomeFirstResponder];
NSLog(@"Tag value of Textfield: %ld",(long)myTextfield.tag);
NSString *text = [(UITextField *)[self.view viewWithTag:myTextfield.tag] text];
NSLog(@"Value: %@", text);
}
但每当我单击其中一个按钮时,它都会返回标记值=4 并且启用第 4 个文本字段以进行编辑。有什么问题?
最佳答案
您正在使用 myTextfield.tag
值来启用文本字段而不是单击按钮的标签。myTextfield
指向最后分配的 UITextField
(标签为 4),这就是为什么您将标签值为 4 并且最后一个文本字段被启用。
问题在于此代码:
myTextfield = (UITextField *)[self.view viewWithTag:myTextfield.tag];
将其更改为:
myTextfield = (UITextField *)[self.view viewWithTag:sender.tag];
注意:
如果按钮和文本框都放在同一个 View 中,请不要对它们使用相同的标签。如果这样做,您将无法确保在使用 viewWithTag
时将获得哪个对象。
关于ios - 在ios中使用标签动态生成和访问UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420457/
我是一个为 iOS 设备开发的新手。我在 InterfaceBuilder 上插入了一个 UITextField,并分配了代码: @interface ComposeViewController :
我有两个要用于登录的 UITextFields,一个用于用户名,一个用于密码。在我无法关闭第一个 UITextField 中的键盘之前,但我设法解决了这个问题,现在的问题是,一旦它关闭,我就无法单击另
在 tvOS 中编辑 UITextField 会显示一个新 View ,用户可以在其中输入文本,文本输入完成后,用户将返回到之前的 View 。但是,我发现当我从文本编辑器返回时,我编辑的文本没有显示
我在同一个 ViewController 上有三个 UITextfields(customerRequestTextField 、 fundsAvailableTextField 和 amountOw
我有一个要求,需要将内容从一个 UITextField 复制到同一屏幕上的另一个 UITextField 中。它就像一个别名,一旦用户开始输入,我就应该开始复制内容。我正在使用方法 (BOOL)tex
我有一个 ViewController、许多 UITextField(一个在另一个下面)和第一个 UITextField 右边的按钮。 按下按钮时,我希望在此下方显示另一个 UITextField(每
我有 3 个 UITextField(位置、地址、 zip )。我在 viewDidLoad 中隐藏了 2 个字段。 Addres1.hidden = YES; Zip1.hidden = YES;
在 Swift 中,我有一个 UITextField、textA 和一个 UITextField、textB。 textA 有时会被禁用,但 textB 始终启用。 textA 被禁用: textA.
我有 2 个 UITextFields。一个是美元,另一个是瑞尔。当我输入 textfield$ 时,我想自动查看 textfieldRiel 中的值。怎么做 ? 最佳答案 实现 uitextfiel
我想将一个 UITextField 属性转移到另一个 UITextField。我该怎么做? 这是我的完整场景: 我创建了一个自定义单元格,其中有一个 UITextField。 现在,当我在我的 UIT
我有一个 UITextField,我想将其长度限制为 4 个字符,这是它的代码: func textField(textField: UITextField, shouldChangeCharacte
@IBOutlet weak var labelLabel: UILabel @IBOutlet weak var textFieldField: UITextField @IBAction func
我在 xib 中定义了一个 UITableViewCell。这个 UITableViewCell 有一个 UITextField,叫做 itemText。此 UITableViewCell 也采用其他
我们的应用程序有几个 UITextField,用户可以在其中输入字母数字输入代码,例如“AA149873A”。 我们希望画外音将这些读为“A A 1 4 9 8 7 2 A”,但它却将数字读为数字:“
在我的 iOS 应用程序中,我有一个文本字段,其中有一个分配给它的 inputView 的选择器 View 。在 iOS 设备上,每当用户点击文本字段时,选择器 View 就会弹出。但是,当我将它作为
在 iOS 13 中,访问 UITextField 时发生崩溃_placeholderLabel.textColor 标签键。 用于应用占位符文本颜色的键。 [textfield setValue:[
我想复制iMessage结构的效果,点击工具栏中的UITextView,键盘就会出现。但我不知道如何在打字时显示文本。我认为当键盘出现时我应该将 UITextField 向上移动 class View
我有两个 UITextField,cDiseasePicker 和 optionalLMS。 我的cDiseasePicker使用UIPickerView作为输入,选项为“否”、“是,1VD”、“是,
当点击某个 CA 层时,我动态创建了两个 UITextField。我已将第一个文本字段设为第一响应者,我想要的是,当我在第一个文本字段中输入文本并按完成时,我希望第二个文本字段成为第一响应者。这就是我
我正在寻找 UITextField 的方法,它在我输入 UITextField 后产生效果。 我有一个方法来进行计算我使用一个按钮,但我不想使用,我希望你在我在 UITextField 中输入数字后运
我是一名优秀的程序员,十分优秀!