ios Swift - 从 viewController 访问 AppDelegate 窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30027780/
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-08-31 05:52:59  来源:igfitidea点击:

Swift - Accessing AppDelegate window from viewController

iosswiftuiviewcontrollerappdelegate

提问by theDC

I make walkthrough (onboarding flow) in my app and I'd like to have a skip button. The button is located on viewController, so I figured out that the best way to move to another viewController would be access app delegate window.

我在我的应用程序中进行了演练(入职流程),并且我想要一个跳过按钮。该按钮位于 viewController 上,所以我发现移动到另一个 viewController 的最佳方法是访问应用程序委托窗口。

However, it keeps getting me an error that AppDelegate.Type does not have a member called "window".

但是,它不断给我一个错误,即 AppDelegate.Type 没有名为“window”的成员。

@IBAction func skipWalkthrough(sender: AnyObject) {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    AppDelegate.window!.rootViewController = RootViewController   
}

Is there anything wrong with such approach?

这种方法有什么问题吗?

Thanks in advance!

提前致谢!

回答by Stefan Salatic

You have a typo it is supposed to be appDelegatenot AppDelegate. So like this:

你有一个错字,它应该appDelegate不是AppDelegate。所以像这样:

@IBAction func skipWalkthrough(sender: AnyObject) {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegate.window!.rootViewController = RootViewController   
}

Swift 3.2

斯威夫特 3.2

@IBAction func skipWalkthrough(_ sender: AnyObject) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window!.rootViewController = controller
    }

回答by Dasoga

This is for with or without Storyboard and it is working for Swift 3+

这适用于有或没有 Storyboard,它适用于Swift 3+

let appDelegate = UIApplication.shared.delegate as? AppDelegate
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeController = mainStoryboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
appDelegate?.window?.rootViewController = homeController

回答by Ted Lowery

Swift 3

斯威夫特 3

This is a better way:

这是一个更好的方法:

    if let window = NSApplication.shared().windows.first {
        window.acceptsMouseMovedEvents = true;
    }

回答by giorashc

You are using the protocol name (i.e. AppDelegate) instead of the instance:

您正在使用协议名称(即AppDelegate)而不是实例:

Should be:

应该:

appDelegate.window!.rootViewController = RootViewController   

回答by Gustavo Vollbrecht

You can also use conditional binding to reach the window.

您还可以使用条件绑定来访问window.

if let window = UIApplication.shared.windows.first {
    // use window here.
}

回答by Umit Kaya

You can access tab bar anywhere from the app. Use below:

您可以从应用程序的任何位置访问标签栏。下面使用:

let appDelegate = UIApplication.shared.delegate as! AppDelegate

if let tabBarController = appDelegate.window!.rootViewController as? UITabBarController {
    if let tabItems = tabBarController.tabBar.items {
       let tabItem = tabItems[2]
       tabItem.badgeValue = "5" //enter any value
    }
}

回答by Humza Shahid

This solution work for : After Login / Register Programmatically add UITabbarController

此解决方案适用于:登录/注册后以编程方式添加 UITabbarController

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window!.rootViewController = tabs
appDelegate.window!.makeKeyAndVisible()