我的主视图 Controller 中有一个文本字段。我正在使用 FPPopover在我的主视图 Controller 之上显示另一个 View Controller 。我想从 FPPopoverController 获取用户的名称,并在我的主视图 Controller 中放入一个文本字段。我尝试了以下但没有用..
MasterViewController *vc = [[MasterViewController alloc] init];
vc.nameTextField.text = self.nameField.text;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
MasterViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"MasterViewController"];
vc.nameTextField.text = self.nameField.text;
我还能尝试什么? 谢谢。
最佳答案
委托(delegate)是我认为最好的方法,但最简单的方法是使用通知。
MyPopoverViewController 实现
- (void)doneButtonPressed:(id)sender{
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification"
object:self
userInfo:@{@"aKey":self.textfield.text} ];
}
在你的主视图 Controller viewdidload方法中
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateTextField:)
name:@"notification"
object:nil];
最后添加这个方法..
- (void)updateTextField:(NSNotification *)notification {
self.nameTextField.text = [notification.userInfo objectForKey:@"aKey"];
}
关于ios - 从不同的 ViewController iOS 更新 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16878613/