簡體   English   中英

如何使用 Objective-C 在 iOS 13 中獲取 rootViewController?

[英]How to get rootViewController in iOS 13 using Objective-C?

我正在嘗試使用 Objective-C 獲取rootViewController 13 中的 rootViewController。 我正在做這樣的事情:

for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
    UIWindowScene *windowScene = (UIWindowScene *) scene;
    UIWindowSceneDelegate *windowSceneDelegate = (UIWindowSceneDelegate *) windowScene.delegate;
    windowSceneDelegate.window = ...
}

但是當我嘗試訪問 windowSceneDelegate.window = 中的window屬性時windowSceneDelegate.window = (獲取rootViewController )我收到以下錯誤:

在前向 class object 'UIWindowSceneDelegate' 中找不到屬性“窗口”

但是當我 go 定義UIWindowSceneDelegate時,我看到了window屬性:

在此處輸入圖像描述

那么使用 Objective-C 在 iOS 13 中獲取 rootViewController 的正確方法是什么?

將您的代碼更改為:

for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
    if ([scene.delegate conformsToProtocol:@protocol(UIWindowSceneDelegate)]) {
        UIWindow *window = [(id<UIWindowSceneDelegate>)scene.delegate window];
    }
}

當您打開UIKit s UIWindowScene.h header 文件時,它包含:

@class UIScreen, UIWindow, UIWindowSceneDelegate, UISceneDestructionRequestOptions, CKShareMetadata, UISceneSizeRestrictions;

看,有UIWindowSceneDelegate 這是前向聲明。

閱讀此答案以了解前向聲明是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM