【问题标题】:Pass a NSString from Child to Parent for ModalViewController将 NSString 从 Child 传递给 Parent 用于 ModalViewController
【发布时间】:2011-05-06 17:31:00
【问题描述】:

我的父 ViewController 类包含一个 UISearchBar。我有一个 UIButton,它将通过下面的代码推送一个新的 ViewController

AddViewController *addViewController = [[AddViewController alloc] initWithNibName:@"AddView" bundle:nil];
[self presentModalViewController:addViewController animated:YES];
[addViewController release];

在 AddViewController 中,用户会看到动物名称的 TableView。在

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

我提取动物名称并将其存储在 NSString 中。然后我通过

关闭 AddViewController
[self dismissModalViewControllerAnimated:YES];

我的问题是如何将 NSString 动物名称传递给 AddViewController 的父 ViewController?我正在尝试将动物名称放在 UISearchBar

【问题讨论】:

    标签: iphone objective-c uiviewcontroller modalviewcontroller


    【解决方案1】:

    您需要创建一个委托。

    http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

    这些允许您与父视图控制器进行通信,并在您的情况下传递一个变量。

    【讨论】:

    • 我不会说“需要” - 这是你可以的一种方式。 :-)
    • 没有其他方法可以干净地做到这一点。如果需要传递多个变量,最好使用委托。
    【解决方案2】:

    您可以在父视图控制器中创建一个属性,然后您可以通过调用从子视图控制器更新该属性

    self.parentViewController.searchBarText = animalNameString;
    

    当孩子被解雇时,在父母的 viewWillAppear 方法中设置 UISearchBar 文本

    【讨论】:

      猜你喜欢
      • 2018-03-12
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      相关资源
      最近更新 更多