是否可以从appDelegate访问ViewController的方法,就像可能与以下代码相反:?
AppDelegate *ad = (AppDelegate *) [[UIApplication sharedApplication] delegate];
当我尝试
时ViewController *vc = (ViewController *) [[UIApplication sharedApplication] delegate];
我收到错误...
谢谢!
答案 0 :(得分:2)
在AppDelegate
中,假设viewController
是您的rootViewController
,您可以通过以下方式获取堆栈中所有视图控制器的列表:
NSLog(@"List of current active view controllers:%@", self.viewController.navController.viewControllers);
访问viewControllers中的特定视图控制器:
[[self.viewController.navController.viewControllers objectAtIndex:i] someMethodOfThatViewController];
答案 1 :(得分:1)
ViewController *vc = (ViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
或者您的代表是否具有属性
YourAppDelegate *ad = (YourAppDelegate*)[UIApplication sharedApplication].delegate;
ViewController *vc = ad.yourViewControllerProperty;