gpt4 book ai didi

ios - 当我单击 Objective C 中的按钮时,为什么未从 tableViewCell 获取文本字段数据?

转载 作者:行者123 更新时间:2023-11-29 00:28:37 25 4
gpt4 key购买 nike

如果我点击按钮 NSLog 显示 NULL 值,它没有从文本字段获取数据,为什么?......

委托(delegate)方法正常工作................................................

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
if (indexPath.row == 0) {
self.nameTextField = [[UITextField alloc]init];
self.nameTextField.frame =CGRectMake(25, 0, 300, 50);
self.nameTextField.placeholder = @"Name";
self.nameTextField.keyboardType = UIKeyboardTypeAlphabet;
[cell addSubview:self.nameTextField];
self.nameTextField.delegate = self;

return cell;
}
if (indexPath.row == 1) {

self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame = CGRectMake(100, 10, 135, 50);
[self.button setTitle:@"StoreData" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:self.button];

return cell;
}

return nil;
}

-(void)buttonClicked:(id) sender {

if (sender == self.phoneTextField) {

self.nameString = self.nameTextField.text;
}

NSLog(@"Name : %@", self.nameString);
}

最佳答案

Action 函数中的参数是按钮而不是textField

-(void)buttonClicked:(id) sender {
if (sender == self.button) {
self.nameString = self.nameTextField.text;
}
NSLog(@"Name : %@", self.nameString);
}

关于ios - 当我单击 Objective C 中的按钮时,为什么未从 tableViewCell 获取文本字段数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42418665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com