xcode “AppDelegate”类型的值没有成员“window”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33714671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:12:30  来源:igfitidea点击:

Value of type 'AppDelegate' has no member 'window'

iosxcodeswiftswift2xcode7

提问by Ben77006

Im trying to use the 3D touch Quick Actions and I'm setting it up copying VEA Software code. In his sample code it works perfectly but when I try to add it to my app I get some unusual errors. I am new to coding and swift so please explain as much as possible. Thanks. Below I have the code which is in my app delegate.

我正在尝试使用 3D 触摸快速操作,我正在设置它复制 VEA 软件代码。在他的示例代码中,它工作得很好,但是当我尝试将它添加到我的应用程序时,我遇到了一些不寻常的错误。我是编码和 swift 的新手,所以请尽可能多地解释。谢谢。下面我有我的应用程序委托中的代码。

This is where I'm getting the error (self.window?):

这是我收到错误的地方(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

}

回答by Amitha

In iOS 13, Xcode 11, the sceneDelegate handles the functionality of UIScene and window. The window performs properly when used in the sceneDelegate.

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

回答by Talha Rasool

When you are using Scene view then windows object is in the scene delegate. Copy window object and place it in the app delegate and it will work.

当您使用场景视图时,窗口对象位于场景委托中。复制窗口对象并将其放置在应用程序委托中,它将起作用。

回答by Johnd

Sometimes when you have an issue in AppDelegate it can be solved with a quick Product -> Clean. Hope this helps.

有时,当您在 AppDelegate 中遇到问题时,可以通过快速的 Product -> Clean 来解决。希望这可以帮助。

回答by Ahmed Ayad

first you need to specify which shortcut you need to use , cuz there are two types of shortcuts : dynamic and static and let say that you choose the static that can be done and added from the property list info then you have to handle the actions for each shortcut in the app delegate file in your project

首先,您需要指定您需要使用哪个快捷方式,因为有两种类型的快捷方式:动态和静态,假设您选择可以从属性列表信息中完成和添加的静态,然后您必须处理以下操作项目中应用程序委托文件中的每个快捷方式

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
                }
            }
        }
    }



}

this will be work so fine for you but first you have to determine the rootViewController we can say like :

这对您来说非常有用,但首先您必须确定我们可以这样说的 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
                }