ios - 从不同的 ViewController iOS 更新 UITextField

标签 ios objective-c uiviewcontroller uistoryboard data-sharing

我的主视图 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/

相关文章:

ios - 在 Swift 的 UIViewController 中添加 UITableView 的问题

iphone - 核心数据获取到许多

ios - 集成 google map api iOS 时 map 未加载

ios - 如何在 iOS 中使用 Google map API 在 mapView 上获取触摸位置的纬度和经度

ios - 以编程方式自动布局不起作用

ios - 如何在透视中绘制图像?

ios - 更改嵌套 NSMutableDictionary 中的值

objective-c - 使用 Cocoa 解析 XML 数据

ios - NSFetchedResultsController:在后台线程中获取

uiviewcontroller - iOS8 中的 MFMessageComposeViewController 问题