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