有时候开发需要获取最顶端的控制器来做一些事情,例如在APPDelegate里获取最顶端控制器来present手势密码等,一下是代码:
UIWindow * presentWindow = [UIApplication sharedApplication].windows[0];
UIViewController *toRootViewController = presentWindow.rootViewController;
while (toRootViewController.presentedViewController)
{ toRootViewController = toRootViewController.presentedViewController;
}