iPhone - 在选项卡栏中切换视图控制器并调用方法
我的应用程序中有一个 TabBar,我在 AppDelegate 中执行此操作:
...
test2ViewController = [[Test1ViewController alloc] init];
...
navigationTest2Controller = [[UINavigationController alloc] initWithRootViewController:test2ViewController];
NSArray *myControllers = [NSArray arrayWithObjects:..., navigationTest2Controller, nil];
[self.myTabBarController setViewControllers:myControllers animated:NO];
现在我遇到的问题是我位于 ViewController 中,并且我想切换到“navigationTest2Controller”。我在我的 AppDelegate 中执行此操作:
self.myTabBarController.selectedViewController = navigationTest2Controller;
这有效。它切换到这个 ViewController!这个ViewController已经被加载并且viewDidLoad方法被调用。在此 viewDidLoad 方法中是一个方法调用:
[self myMethod];
我希望,如果视图切换到此 ViewController,则应始终调用此“myMethod”。 我该怎么做?在我的 AppDelegate 行之前
self.myTabBarController.selectedViewController = navigationTest2Controller;
???或者是否有另一个委托在每次选择/切换到 ViewController 时都会被调用?
有人知道这个吗?
预先非常感谢&此致。
I have a TabBar in my application and I do this in my AppDelegate:
...
test2ViewController = [[Test1ViewController alloc] init];
...
navigationTest2Controller = [[UINavigationController alloc] initWithRootViewController:test2ViewController];
NSArray *myControllers = [NSArray arrayWithObjects:..., navigationTest2Controller, nil];
[self.myTabBarController setViewControllers:myControllers animated:NO];
Now I have the problem that I am in a ViewController and I want to switch to the "navigationTest2Controller". I do this in my AppDelegate with:
self.myTabBarController.selectedViewController = navigationTest2Controller;
This works. It switches to this ViewController! This ViewController was already loaded and the viewDidLoad method was called. In this viewDidLoad method is a methos call:
[self myMethod];
I want, that if the view switches to this ViewController this "myMethod" should always be called.
How can I do this? In my AppDelegate before the line
self.myTabBarController.selectedViewController = navigationTest2Controller;
??? Or is there another delegate which will be called every time the ViewController is selected/switched to?
Does anyone know this?
Thanks a lot in advance & Best Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将视图可见时要运行的任何代码放置在
-viewWillAppear:
或-viewDidAppear
视图控制器的方法。编辑:要使这种情况发生仅当您从某个视图切换时,您可以子类化
UITabBarController
,仅修改< code>–tabBarController:shouldSelectViewController: 方法。在该方法中,您可以执行以下操作:You should place any code that you want to run when the view becomes visible in the
-viewWillAppear:
or-viewDidAppear
method of your view controller.EDIT: To make this happen only when you switch from a certain view, you can subclass
UITabBarController
, only modifying the–tabBarController:shouldSelectViewController:
method. In that method, you could do something like this: