簡體   English   中英

“AppDelegate”類型的值沒有成員“window”

[英]Value of type 'AppDelegate' has no member 'window'

我正在嘗試使用 3D 觸摸快速操作,我正在設置它復制 VEA 軟件代碼。 在他的示例代碼中,它工作得很好,但是當我嘗試將它添加到我的應用程序時,我遇到了一些不尋常的錯誤。 我是編碼和快速的新手,所以請盡可能多地解釋。 謝謝。 下面我有我的應用程序委托中的代碼。

這是我收到錯誤的地方( self.window? ):

@available(iOS 9.0, *)
func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) -> Bool
{
var handled = false
var window: UIWindow?

guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else { return false }
guard let shortcutType = shortcutItem.type as String? else { return false }

switch (shortcutType)
{
case ShortcutIdentifier.First.type:
    handled = true
    var window = UIWindow?()

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let navVC = storyboard.instantiateViewControllerWithIdentifier("ProjectPage") as! UINavigationController
    // Error on line below
    self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)


    break
case ShortcutIdentifier.Second.type:
    handled = true

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let navVC = storyboard.instantiateViewControllerWithIdentifier("ContactPageView") as! UINavigationController
    // Error on line below
    self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
    break

case ShortcutIdentifier.Third.type:
    handled = true

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let navVC = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! UINavigationController
    // Error on line below
    self.window?.rootViewController?.presentViewController(navVC, animated: true, completion: nil)
    break


default:
    break
}

return handled

}

在 iOS 13、Xcode 11 中,sceneDelegate 處理 UIScene 和 window 的功能。 窗口在sceneDelegate 中使用時正常執行。

Xcode 12.0 斯威夫特 5.0

目前你需要:

  1. 從 info.plist 文件中刪除“應用場景清單”;
  2. 移除場景委托類;
  3. 移除 AppDelegate 類中的場景相關方法;
  4. 如果丟失,添加屬性var window: UIWindow? 到您的 AppDelegate 類。

func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?)添加一些邏輯。

需要支持 iOS 12 和 13 時的實現示例:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        configureInitialViewController()
        return true
    }

    private func configureInitialViewController() {
        let initialViewController: UIViewController
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        window = UIWindow()

        if (condition) {
            let mainViewController = storyboard.instantiateViewController(withIdentifier: "mainForm")
            initialViewController = mainViewController
        } else {
            let loginViewController = storyboard.instantiateViewController(withIdentifier: "loginForm")
            initialViewController = loginViewController
        }
        window?.rootViewController = initialViewController
        window?.makeKeyAndVisible()
    }
}

當您使用場景視圖時,窗口對象位於場景委托中。 復制窗口對象並將其放置在應用程序委托中,它將起作用。

如果您的項目已經有SceneDelegate文件,那么您需要在AppDelegate使用它,就像這樣:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    guard let _ = (scene as? UIWindowScene) else { return }
    
        
    
    let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = mainStoryBoard.instantiateViewController(withIdentifier: "setCountry")
    window?.rootViewController = viewController
}

有時,當您在 AppDelegate 中遇到問題時,可以通過快速的 Product -> Clean 來解決。 希望這可以幫助。

這對我有用。

在 SceneDelegate 的第一個函數func scene(...) 、 if 語句、 if let windowScene = scene as? UIWindowScene {..Add Below Code Here..}添加以下代碼 if let windowScene = scene as? UIWindowScene {..Add Below Code Here..} .

確保在文件頂部添加import MediaPlayer

let volumeView = MPVolumeView()
volumeView.alpha = 0.000001
window.addSubview(volumeView)

最后你的代碼應該是:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    

    // Create the SwiftUI view that provides the window contents.
    let contentView = ContentView()

    // Use a UIHostingController as window root view controller.
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()

        // MARK:    Hide Volume HUD View
        let volumeView = MPVolumeView()
        volumeView.alpha = 0.000001
        window.addSubview(volumeView)
    }
}

首先,您需要指定您需要使用哪個快捷方式,因為有兩種類型的快捷方式:動態和靜態,假設您選擇可以從屬性列表信息中完成和添加的靜態,然后您必須處理以下操作項目中應用程序委托文件中的每個快捷方式

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let mainTabController = storyboard.instantiateViewController(withIdentifier: "Tab_Bar") as? Tab_Bar
    let loginViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC") as? LoginVC
    let doctor = storyboard.instantiateViewController(withIdentifier: "DrTabController") as! DrTabController
    getData(key: "token") { (token) in
        getData(key: "type") { (type) in
            if token != "" && type != "" , type == "2" {
                self.window?.rootViewController = mainTabController
                if #available(iOS 13.0, *) {
                    self.window?.overrideUserInterfaceStyle = .light
                } else {
                    self.window?.makeKeyAndVisible()
                }
                if shortcutItem.type == "appointMent" {
                    mainTabController?.selectedIndex = 1
                } else if shortcutItem.type == "Chatting" {
                    mainTabController?.selectedIndex = 2
                }
            } else if token != "" && type != "" , type == "1"{
                self.window?.rootViewController = doctor
                if #available(iOS 13.0, *) {
                    self.window?.overrideUserInterfaceStyle = .light
                } else {
                    self.window?.makeKeyAndVisible()
                }
                if shortcutItem.type == "appointMent" {
                    mainTabController?.selectedIndex = 1
                } else if shortcutItem.type == "Chatting" {
                    mainTabController?.selectedIndex = 2
                }
            } else if token == "" && type == "" {
                self.window?.rootViewController = loginViewController
                if #available(iOS 13.0, *) {
                    self.window?.overrideUserInterfaceStyle = .light
                } else {
                    self.window?.makeKeyAndVisible()
                }
                if shortcutItem.type == "appointMent" {
                    mainTabController?.selectedIndex = 1
                } else if shortcutItem.type == "Chatting" {
                    mainTabController?.selectedIndex = 2
                }
            }
        }
    }



}

這對你來說很好用,但首先你必須確定 rootViewController 我們可以這樣說:

        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

  self.window?.rootViewController = mainTabController
                if #available(iOS 13.0, *) {
                    self.window?.overrideUserInterfaceStyle = .light
                } else {
                    self.window?.makeKeyAndVisible()
                }
                if shortcutItem.type == "appointMent" {
                    mainTabController?.selectedIndex = 1
                } else if shortcutItem.type == "Chatting" {
                    mainTabController?.selectedIndex = 2
                }

暫無
暫無

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

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