我正在开发基于视图的应用程序。在这我有两个视图控制器。它们是:Rootviewcontroller 和 Detailviewcontroller。
现在我使用 pushViewController 方法将值传递给 Detailviewcontroller。它工作正常。但是现在当我从这个 Detailviewcontroller 中弹出时,我应该访问 Rootviewcontroller 中的 myfunction。请问有可能吗?
任何帮助将不胜感激!
-沙迪亚
我正在开发基于视图的应用程序。在这我有两个视图控制器。它们是:Rootviewcontroller 和 Detailviewcontroller。
现在我使用 pushViewController 方法将值传递给 Detailviewcontroller。它工作正常。但是现在当我从这个 Detailviewcontroller 中弹出时,我应该访问 Rootviewcontroller 中的 myfunction。请问有可能吗?
任何帮助将不胜感激!
-沙迪亚
在根视图控制器的 viewWillAppear:(BOOL)animated 方法或 viewDidAppear:(BOOL)animated 方法中调用 myfunction,具体取决于您希望何时调用它。这样,只要该控制器出现,您需要的代码就会运行。
I don't really understand your question 100%, but I think you want to access the methods of the Root ViewController from another View Controller further up the stack?
You can do something like this in DetailViewController:
RootViewController *rootViewController = (RootViewController*)self.navigationController.topViewController;
Now that you have a reference to the Root, you can call methods on it as you wish.
在 AppDelegate 中存储 rootViewController 的实例。
// AppDelegate.h
RootViewController* rootViewController;
向实现添加访问方法。
// AppDelegate.m
- (UIViewController*) GetRootViewController {
return rootViewController;
}
调用此方法可从应用程序中的任何位置获取 rootViewController。
UIViewController* controller =
[(AppDelegate*)[[UIApplication sharedApplication] delegate] GetRootViewController];