[英]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.