ViewController委托

时间:2012-06-27 11:30:05

标签: objective-c ios xcode uiviewcontroller uiapplicationdelegate

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

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

当我尝试

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

我收到错误...

谢谢!

2 个答案:

答案 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;