【问题标题】:How to access to parentViewController after presentModalViewController?presentModalViewController 后如何访问 parentViewController?
【发布时间】:2012-02-05 23:31:47
【问题描述】:

我需要在 presentMoalViewController 之后访问 parentViewController。 这是我在 firstViewController 中调用以查看 secondViewController 的方法:

- (void)viewData {

    SecondViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:@"select_data"];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
    navigationController.navigationBar.barStyle = UIBarStyleBlack;

    UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(saveData)] autorelease];
    viewCtrl.navigationItem.rightBarButtonItem = salvaButton;

    UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:@"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:@selector(backView)] autorelease];
    viewCtrl.navigationItem.leftBarButtonItem = annullaButton;

    [self presentModalViewController:navigationController animated:YES];

}

当我点击saveButton时,我尝试以这种方式访问​​parentViewController,但它不起作用:

- (void) saveData {

    FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
    parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"];
    [parentView performSelectorInBackground:@selector(myMethod) withObject:nil];

    [self dismissModalViewControllerAnimated:YES];

}

【问题讨论】:

  • myMethod 调用时我没有得到你要做什么?

标签: iphone objective-c ios5 presentmodalviewcontroller


【解决方案1】:

如果您在第二个 viewController 中使用第一个 viewController 可以将自己设置为的委托/协议会更好。您可以在这里找到更多信息Pass data back to the previous controller 或简单地进行搜索。这种设计模式在 iOS 编程中几乎无处不在。

最大的好处是,你的第二个 viewController 变得独立于任何呈现它的人,并且可以很容易地重用。技术术语是“解耦”。

【讨论】:

    【解决方案2】:

    我上次这样做时使用了通知:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"saveData" object:dataString];
    

    在你的父 vc 视图中做:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveDataNow:) name:@"saveData" object:nil];
    

    【讨论】:

      【解决方案3】:

      对于navigationController,您可以声明一个类似于myController 的属性,然后您必须像这样启动您的navigationController

      UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
      navigationController.navigationBar.barStyle = UIBarStyleBlack;
      navigationController.myController = self;
      
      UIBarButtonItem *saveButton = [[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:viewCtrl action:@selector(saveData)] autorelease];
      viewCtrl.navigationItem.rightBarButtonItem = salvaButton;
      
      UIBarButtonItem *undoButton = [[[UIBarButtonItem alloc] initWithTitle:@"Undo" style:UIBarButtonItemStyleBordered target:viewCtrl action:@selector(backView)] autorelease];
      viewCtrl.navigationItem.leftBarButtonItem = annullaButton;
      
      [self presentModalViewController:navigationController animated:YES];
      

      在你的 parentViewController 类中你声明你的方法

      - (void) saveData {
      
      FirstViewController *parentView = (FirstViewController*)[[self navigationController] presentingViewController];
      parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"];
      [parentView performSelectorInBackground:@selector(myMethod) withObject:nil];
      
      [self dismissModalViewControllerAnimated:YES];
      }
      

      然后您可以在您的navigationController 中调用[myController saveData]; 这应该有效,如果您有任何问题,请随时提问!

      【讨论】:

      • 我不明白如何声明myController
      • 问题是[self navigationController] presentingViewController]返回UITabBarViewController
      【解决方案4】:

      您可以使用以下方式访问您的父视图控制器-

      - (void) saveData {
      NSMutableArray *activeControllerArray = [self.navigationController.viewControllers mutableCopy];
      FirstViewController *parentView;
      for(int i = 0, i <[activeControllerArray  count], i++) {
          if([[activeControllerArray objectAtIndex:i] isKindOfClass:[FirstViewController class]) {
              parentView= [activeViewController objectAtIndex:i];
              break;
           }
      }
      
      parentView.dataString = [[NSMutableString alloc] initWithFormat:@"new string"];
      [parentView performSelectorInBackground:@selector(myMethod) withObject:nil];
      
      [self dismissModalViewControllerAnimated:YES];
      
      }
      

      【讨论】:

      • 感谢您的回复,但 activeControllerArray 仅包含 secondViewController :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多