为了便于参考,我如何从app委托中获取当前活动的视图(用户当前正在查看的主视图)?
发布于 2013-06-01 03:12:20
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;
发布于 2011-11-03 01:22:24
这取决于你的应用是如何设置的。
通常,您的应用程序委托将具有一个允许您访问它的主视图控制器属性。
获取你的应用委派
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIView *topView = appDelegate.viewController.view;
如果您的应用程序在应用程序委托中具有导航控制器属性,则可以执行以下操作。
UIView *topView = appDelegate.navigationController.topViewController.view;
发布于 2017-01-13 05:55:49
我发现了这一点,它对我所有类型的segues和视图控制器都很有效。
+(UIViewController*) getTopController{
UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topViewController.presentedViewController) {
topViewController = topViewController.presentedViewController;
}
return topViewController;
}
https://stackoverflow.com/questions/3705410
复制相似问题