【问题标题】:Swift - Accessing AppDelegate window from viewControllerSwift - 从 viewController 访问 AppDelegate 窗口
【发布时间】:2015-07-13 16:48:06
【问题描述】:

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

但是,AppDelegate.Type 没有名为“window”的成员时,我一直收到错误消息。

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

这样的做法有什么问题吗?

提前致谢!

【问题讨论】:

  • 你也可以尝试获取可见的viewcontroller

标签: ios swift uiviewcontroller appdelegate


【解决方案1】:

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

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

Swift 3.2

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

【讨论】:

  • 好吧,多么愚蠢的错误!顺便说一句,现在 RootViewController 有一个问题,如何访问在故事板中声明的这个视图控制器?我想创建一个新实例不是一个好主意?我的意思是 RootViewController() 导致黑屏,所以应该再等待访问这样的控制器
  • 这是我在代码中的操作方式let storyboardId = "LoginStoryboardIdentifier"self.window?.rootViewController = self.window?.rootViewController?.storyboard?.instantiateViewControllerWithIdentifier(storyboardId) as? UIViewController
  • 工作完美,但注意到内存消耗,当我将根视图控制器 (a) 替换为 (b) 时,我没有看到内存减少。这意味着(a)还没有从记忆中消失,对吧?如何将其从内存中擦除 1st 然后显示 (b) 控制器
【解决方案2】:

这适用于有或没有 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

【讨论】:

  • 这在 Swift 5+ 中是否仍然有效,我似乎无法让它工作,行 appDelegate?.window 抛出一个错误,说不能对类型的非可选值使用可选链接任何',修复它会产生其他错误。
【解决方案3】:

斯威夫特 3

这是一个更好的方法:

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

【讨论】:

  • NSApplication is used in MacOS this question is tag in iOS
【解决方案4】:

appDelegate.window!.rootViewControllerSwift 5

中不起作用

这是工作代码

在下面添加扩展

extension UIWindow {
    static var key: UIWindow! {
        if #available(iOS 13, *) {
            return UIApplication.shared.windows.first { $0.isKeyWindow }
        } else {
            return UIApplication.shared.keyWindow
        }
    }
}

使用

let mainSB = UIStoryboard(name: "Main", bundle: nil)
                    
if let RootVc = mainSB.instantiateViewController(withIdentifier: "NavigationController") as? UINavigationController{
    UIWindow.key.rootViewController = RootVc
}

UIWindow.key // to access only window

【讨论】:

  • 这是您在 Swift5、iOS 13 及更高版本中访问 UIWindow 实例以设置它的 rootViewController 或呈现任何 UIView 的方式。谢谢老兄。
  • @Suhail 我忘记添加 1 行 window.makeKeyAndVisible() 请检查我的更新答案,它肯定会工作 :)
【解决方案5】:

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

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

【讨论】:

    【解决方案6】:

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

    应该是:

    appDelegate.window!.rootViewController = RootViewController   
    

    【讨论】:

      【解决方案7】:

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

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

      【讨论】:

        【解决方案8】:

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

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

        【讨论】:

        • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of howwhy 这解决了问题。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-26
        相关资源
        最近更新 更多