简体   繁体   English

引用从AppDelegate中的StoryBoard创建的TabbarController?

[英]Referencing TabbarController created from StoryBoard in AppDelegate?

I have a tabbar app with an initial login screen. 我有一个带有初始登录屏幕的标签栏应用。 The tabbarController is set as the initial view in Storyboard with 1 VC that has a navigationController also embed. tabbarController设置为Storyboard中具有1个VC的初始视图,该VC中还嵌入了navigationController。

I have a loginVC instantiated and set as rootViewController in my AppDelegate. 我实例化了一个loginVC,并在AppDelegate中将其设置为rootViewController。 After the user has successfully sign in from the loginVC, I need to switch over to the tabbarController. 用户成功从loginVC登录后,我需要切换到tabbarController。 Would I try to get a reference to the tabbarcontroller and set it as the new rootviewcontroller? 我会尝试获取对tabbarcontroller的引用并将其设置为新的rootviewcontroller吗? If so, I'm having a hard time figuring out how to do this:/ 如果是这样,我很难弄清楚该怎么做:/

AppDelegate AppDelegate中

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if NSUserDefaults.standardUserDefaults().objectForKey("OAuth") == nil {

        self.window = UIWindow(frame:UIScreen.mainScreen().bounds)

        var storyboard = UIStoryboard(name: "Main", bundle: nil)

        var loginVC = storyboard.instantiateViewControllerWithIdentifier("LOGIN_VC") as LoginVC


        self.window?.rootViewController = loginVC
        self.window?.makeKeyAndVisible()
    } 

    return true
}

This method gets called after user has successfully signed in 用户成功登录后将调用此方法

  func dismissLoginVC() {
    var tabbarController = self.storyboard?.instantiateViewControllerWithIdentifier("TABBAR") as UITabBarController

    self.presentViewController(tabbarController, animated: true, completion: nil)

    let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    appDelegate.window?.rootViewController =  tabbarController

}

I know the problem with this is it just created a new tabbarcontroller, rather than referencing the existing tabbarController that was set as the initialView in storyboard. 我知道问题在于它只是创建了一个新的tabbarcontroller,而不是引用在Storyboard中设置为initialView的现有tabbarController。 This seems to work but it is missing other items from the navigation bar. 这似乎可行,但导航栏中缺少其他项目。

Thanks for pointing me in the right direction! 感谢您指出正确的方向!

I think you should change your app structure. 我认为您应该更改自己的应用结构。 Keep the tab bar controller as the initial view controller in the storyboard, and present modally (with no animation) the login controller from the viewDidAppear method of the controller in the first tab -- it will be the first thing the user sees. 将标签栏控制器保留为情节提要中的初始视图控制器,并在第一个选项卡中以控制器的viewDidAppear方法以模态方式(无动画显示)登录控制器-这将是用户首先看到的内容。 When you dismiss it, you will be back to that controller in the first tab. 当您关闭它时,您将回到第一个选项卡中的该控制器。 With this approach, you don't need any code in the app delegate. 使用这种方法,您在应用程序委托中不需要任何代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM