Iphone 将文本值从一个视图传递到另一个视图
我的表视图控制器中有这个,我想将单元格的文本值传递给模态视图上的UITextFieldIphone 将文本值从一个视图传递到另一个视图,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我的表视图控制器中有这个,我想将单元格的文本值传递给模态视图上的UITextField - (void)buttonTapped:(id)sender { AddName *addName = [[AddName alloc] init]; UITableViewCell *cell = (UITableViewCell*)[sender superview]; NSLog(@"text in buttontapped: %@", cell.textLabel.text)
- (void)buttonTapped:(id)sender {
AddName *addName = [[AddName alloc] init];
UITableViewCell *cell = (UITableViewCell*)[sender superview];
NSLog(@"text in buttontapped: %@", cell.textLabel.text);
addName.nameField.text = cell.textLabel.text;
addName.delegate = self;
[self presentModalViewController:addName animated:YES];
}
NSLog显示正确的单元格文本值
在模态视图的viewDidLoad方法中,我有以下内容。。。文本值永远不会被设置
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"text: %@",nameField.text);
nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
nameField.autocorrectionType = UITextAutocorrectionTypeDefault;
[nameField becomeFirstResponder];
}
发生什么事了
谢谢。您似乎假设模态视图控制器已经加载了它的视图,因为我假设
nameField
是一个子视图。但很明显,在代码试图设置其text
属性时,文本字段及其父视图尚未加载,因此您正在将-setText:
消息发送到nil
相反,将类型为NSString*
的属性添加到模态视图控制器中,并在按钮映射的
实现中设置该属性。然后,在模态视图控制器的-viewwillbeen
方法中,可以从属性中获取值并将其放入文本字段