aoa*_*nfo 52 objective-c uiviewcontroller ios
有没有办法从AppDelegate获取当前视图控制器?我知道有rootViewController,但这不是我想要的.
dev*_*os1 63
如果你的应用程序的根视图控制器是a,UINavigationController你可以这样做:
((UINavigationController*)appDelegate.window.rootViewController).visibleViewController;
Run Code Online (Sandbox Code Playgroud)
同样,如果是a,UITabBarController你可以这样做:
((UITabBarController*)appDelegate.window.rootViewController).selectedViewController;
Run Code Online (Sandbox Code Playgroud)
当然,像这样的显式铸造很脏.更好的方法是使用强类型捕获引用.
Dan*_*yan 27
这可能有所帮助
- (UIViewController *)topViewController{
return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}
if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self topViewController:lastViewController];
}
UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self topViewController:presentedViewController];
}
Run Code Online (Sandbox Code Playgroud)
Swift版本:
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
Run Code Online (Sandbox Code Playgroud)
取自:https: //gist.github.com/snikch/3661188
A.G*_*A.G 14
进行扩展:
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
let moreNavigationController = tab.moreNavigationController
if let top = moreNavigationController.topViewController where top.view.window != nil {
return topViewController(top)
} else if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
if let rootViewController = UIApplication.topViewController() {
//do sth with root view controller
}
Run Code Online (Sandbox Code Playgroud)
小智 11
获取appDelegate对象:
MyAppDelegate *tmpDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Run Code Online (Sandbox Code Playgroud)
由于beryllium建议您可以使用UINavigationController的属性来访问当前的视图控制器.
所以代码看起来像:
id myCurrentController = tmpDelegate.myNavigationController.topViewController;
Run Code Online (Sandbox Code Playgroud)
要么:
NSArray *myCurrentViewControllers = tmpDelegate.myNavigationController.viewControllers;
Run Code Online (Sandbox Code Playgroud)
小智 8
您可以通过查找其presentViewController从rootViewController获取当前视图控制器,如下所示:
UIViewController *parentViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
while (parentViewController.presentedViewController != nil){
parentViewController = parentViewController.presentedViewController;
}
UIViewController *currentViewController = parentViewController;
Run Code Online (Sandbox Code Playgroud)
它适用于我.希望能帮助到你 :)
对于没有使用UINavigationController而是默认视图控制器为a的任何人,UIViewController您可以使用以下命令检查哪个视图控制器处于活动状态(或显示)AppDelegate:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if let rootViewController = self.window!.rootViewController {
if let presentedViewController = rootViewController.presentedViewController {
return presentedViewController.supportedInterfaceOrientations()
}
} // Else current view controller is DefaultViewController
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在检查当前视图控制器,以便为特定视图控制器支持不同的界面方向。对于其他有兴趣使用此方法来支持特定方法的人,应在每个需要特定方向的视图控制器中放置以下内容。
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
Run Code Online (Sandbox Code Playgroud)
注意:此代码是使用Swift 1.2编写的
基于AG 解决方案的Swift 4+语法中的UIApplication 扩展
public extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
let moreNavigationController = tab.moreNavigationController
if let top = moreNavigationController.topViewController, top.view.window != nil {
return topViewController(base: top)
} else if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
Run Code Online (Sandbox Code Playgroud)
示例用法:
if let rootViewController = UIApplication.topViewController() {
//do something with rootViewController
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63784 次 |
| 最近记录: |