如何从应用程序委托中获取iOS应用程序中的活动视图?

时间:2021-04-30 07:33:37

how can i get the currently active view (the main view currently being viewed by the user) from the app delegate for references sake?

如何从应用程序委托中获取当前活动视图(当前正由用户查看的主视图)以供参考?

8 个解决方案

#1


32  

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;

#2


14  

All top answers doesn't work with modal presented views! Use this code instead:

所有最佳答案都不适用于模态呈现的视图!请改用此代码:

UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

#3


9  

It depends on how your app is set up.

这取决于您的应用的设置方式。

Typically, your app delegate will have a main view controller property that will let you get to it.

通常,您的应用程序委托将具有一个主视图控制器属性,可以让您访问它。

To get your app delegate

让您的应用委托

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

UIView *topView = appDelegate.viewController.view;

If your app has a navigation controller property in the app delegate you can do something like this.

如果您的应用在应用代理中具有导航控制器属性,则可以执行以下操作。

UIView *topView = appDelegate.navigationController.topViewController.view;

#4


1  

I found this and it works great for me for all types of segues and view controllers.

我找到了这个,对我来说,它适用于所有类型的segue和视图控制器。

+(UIViewController*) getTopController{
    UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topViewController.presentedViewController) {
        topViewController = topViewController.presentedViewController;
    }

    return topViewController;
}

#5


0  

you can use [appDelegate.navigationController.topViewController class] to log the class of the controller or get the title property to know which one it is.

您可以使用[appDelegate.navigationController.topViewController类]来记录控制器的类或获取title属性以了解它是哪一个。

#6


0  

Hope this helps you

希望这对你有所帮助

UINavigationController *navCnt = (UINavigationController *)self.window.rootViewController;

if([navCnt.topViewController isMemberOfClass:[WebViewController class]])
{
    return UIInterfaceOrientationMaskAll;
}
else
return UIInterfaceOrientationMaskPortrait;

#7


0  

Anywhere in the app you can get your rootViewController View also like that:

在应用程序中的任何位置,您都可以获得rootViewController视图,如下所示:

id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
UIView *rootView = appDelegate.window.rootViewController.view;

#8


0  

Try Eric's answer:

试试Eric的回答:

+ (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}

#1


32  

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;

#2


14  

All top answers doesn't work with modal presented views! Use this code instead:

所有最佳答案都不适用于模态呈现的视图!请改用此代码:

UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

UIView * topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];

#3


9  

It depends on how your app is set up.

这取决于您的应用的设置方式。

Typically, your app delegate will have a main view controller property that will let you get to it.

通常,您的应用程序委托将具有一个主视图控制器属性,可以让您访问它。

To get your app delegate

让您的应用委托

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

UIView *topView = appDelegate.viewController.view;

If your app has a navigation controller property in the app delegate you can do something like this.

如果您的应用在应用代理中具有导航控制器属性,则可以执行以下操作。

UIView *topView = appDelegate.navigationController.topViewController.view;

#4


1  

I found this and it works great for me for all types of segues and view controllers.

我找到了这个,对我来说,它适用于所有类型的segue和视图控制器。

+(UIViewController*) getTopController{
    UIViewController *topViewController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topViewController.presentedViewController) {
        topViewController = topViewController.presentedViewController;
    }

    return topViewController;
}

#5


0  

you can use [appDelegate.navigationController.topViewController class] to log the class of the controller or get the title property to know which one it is.

您可以使用[appDelegate.navigationController.topViewController类]来记录控制器的类或获取title属性以了解它是哪一个。

#6


0  

Hope this helps you

希望这对你有所帮助

UINavigationController *navCnt = (UINavigationController *)self.window.rootViewController;

if([navCnt.topViewController isMemberOfClass:[WebViewController class]])
{
    return UIInterfaceOrientationMaskAll;
}
else
return UIInterfaceOrientationMaskPortrait;

#7


0  

Anywhere in the app you can get your rootViewController View also like that:

在应用程序中的任何位置,您都可以获得rootViewController视图,如下所示:

id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
UIView *rootView = appDelegate.window.rootViewController.view;

#8


0  

Try Eric's answer:

试试Eric的回答:

+ (UIViewController*) topMostController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}