如何使用 UIApplication 获取我的 UIWindow?

发布于 2024-09-25 03:53:35 字数 489 浏览 12 评论 0原文

我只有一个窗口,我尝试过

UIWindow* mWindow = [[UIApplication sharedApplication] keyWindow];

,但返回为零。

我也尝试过:

UIWindow* mWindow = (UIWindow*)[[UIApplication sharedApplication].windows objectAtIndex:0];

但这引发了异常,应用程序关闭,当我尝试打印时,

[[UIApplication sharedApplication].windows count]

它打印了 0

注意:我将其放入我唯一的视图控制器的 viewDidLoad 方法中,这完全是一个新的基于 iPad 视图的应用程序,所以我没有更改任何内容,只是想获取窗口

请帮我获取这个对象

I have only one window and I tried

UIWindow* mWindow = [[UIApplication sharedApplication] keyWindow];

but this returned nil.

I also tried:

UIWindow* mWindow = (UIWindow*)[[UIApplication sharedApplication].windows objectAtIndex:0];

But this raised an exception and the app closed, when I tried to print out

[[UIApplication sharedApplication].windows count]

It printed 0

Note: I am putting this in my only view controller's viewDidLoad method and this is completely a new iPad View Based Application so I changed nothing, just trying to get the window

Please help me to get this object

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦途 2024-10-02 03:53:35

如果您的主窗口是 AppDelegate 的出口(应该是这种情况),您可以简单地使用

MyAppDelegate* myDelegate = (((MyAppDelegate*) [UIApplication sharedApplication].delegate));
[myDelegate.window ...]

If your main window is an outlet of your AppDelegate (which should be the case), you may simply use

MyAppDelegate* myDelegate = (((MyAppDelegate*) [UIApplication sharedApplication].delegate));
[myDelegate.window ...]
瘫痪情歌 2024-10-02 03:53:35

最简单的方法是从应用程序委托获取窗口:

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
// Do something with the window now

Easiest way is to get the window from the app delegate instead:

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
// Do something with the window now
迷离° 2024-10-02 03:53:35

在应用委托中调用 [window makeKeyAndVisible] 之前,不会设置应用程序的关键窗口。在此调用之前,您的 UIViewController 可能已从 NIB 加载。这解释了为什么 keyWindow 返回 nil。

幸运的是,您的视图控制器不需要通过 UIApplication 来获取窗口。你可以这样做:

UIWindow *mWindow = self.view.window;

Your application's key window isn't set until [window makeKeyAndVisible] gets called in your app delegate. Your UIViewController is probably being loaded from a NIB before this call. This explains why keyWindow is returning nil.

Luckily, your view controller doesn't need to go through UIApplication to get the window. You can just do:

UIWindow *mWindow = self.view.window;
趴在窗边数星星i 2024-10-02 03:53:35
[[[UIApplication sharedApplication] windows] objectAtIndex:0]; // You can also check the count of this to make sure, because if there are no windows it will crash.
[[[UIApplication sharedApplication] windows] objectAtIndex:0]; // You can also check the count of this to make sure, because if there are no windows it will crash.
冷…雨湿花 2024-10-02 03:53:35

对于 iOS 13+,您应该使用:

if let currentScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
    let window = currentScene.keyWindow {
    // now window contains the active window
}

With iOS 13+ you should use:

if let currentScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
    let window = currentScene.keyWindow {
    // now window contains the active window
}
﹉夏雨初晴づ 2024-10-02 03:53:35

您可以通过多种方式实现这一目标。我喜欢使用以下之一。

当您只需要访问一次时,请使用此选项。

if let window = UIApplication.shared.windows.first as UIWindow? {
    // Action
}

如果您需要多次访问 UIWindow,我建议扩展一个类。我喜欢扩展 UIViewController

extension UIViewController {
    /// Returns the UIWindow if available.
    public var window: UIWindow? {
        UIApplication.shared.windows.first as UIWindow?
    }
}

You can achieve this in multiple ways. I like to use one of the following.

Use this when you need to access it just once.

if let window = UIApplication.shared.windows.first as UIWindow? {
    // Action
}

If you need to access the UIWindow multiple times, I would suggest extending a class. I like to extend the UIViewController.

extension UIViewController {
    /// Returns the UIWindow if available.
    public var window: UIWindow? {
        UIApplication.shared.windows.first as UIWindow?
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文