0

是否可以从 appDelegate 访问 ViewController 的方法,就像可以与以下代码相反:?

AppDelegate *ad = (AppDelegate *) [[UIApplication sharedApplication] delegate];

当我尝试

ViewController *vc = (ViewController *) [[UIApplication sharedApplication] delegate];

我收到一个错误...

谢谢!

4

2 回答 2

2

在您的AppDelegate中,假设viewController是您的rootViewController,您可以通过以下方式获取堆栈上所有视图控制器的列表:

NSLog(@"List of current active view controllers:%@", self.viewController.navController.viewControllers);

要访问 viewControllers 中的特定视图控制器:

[[self.viewController.navController.viewControllers objectAtIndex:i] someMethodOfThatViewController]; 
于 2012-06-27T12:56:37.503 回答
1
ViewController *vc = (ViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;

或者如果您的代表有属性

YourAppDelegate *ad = (YourAppDelegate*)[UIApplication sharedApplication].delegate;
ViewController *vc = ad.yourViewControllerProperty;
于 2012-06-27T11:43:05.340 回答