swift2 类型'AppDelegate'的值没有成员'window'

jv4diomz  于 2022-11-06  发布在  Swift
关注(0)|答案(7)|浏览(401)

我试图使用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

}
of1yzvn4

of1yzvn41#

Xcode 12.0快速移动5.0

目前,您需要:
1.从info.plist文件中删除“应用程序场景清单”;
1.删除场景委托类;
1.删除AppDelegate类中与场景相关的方法;
1.如果缺少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()
    }
}
kg7wmglp

kg7wmglp2#

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

uhry853o

uhry853o3#

当你使用场景视图时,窗口对象在场景代理中。复制窗口对象并将其放置在应用程序代理中,它将工作。

9jyewag0

9jyewag04#

如果你的项目已经有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
}
nzk0hqpo

nzk0hqpo5#

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

zlwx9yxi

zlwx9yxi6#

这对我很有效。
将以下代码添加到SceneDelegate的第一个函数func scene(...)的if语句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)
    }
}
p4rjhz4m

p4rjhz4m7#

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

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
                }

相关问题