如何从导航控制器切换到选项卡控制器并使其成为根视图控制器

Posted

技术标签:

【中文标题】如何从导航控制器切换到选项卡控制器并使其成为根视图控制器【英文标题】:How to segue from a nav controller to tab controller and make it root view controller 【发布时间】:2016-10-31 00:32:14 【问题描述】:

您好,当用户登录或想要注册应用程序时,我在导航控制器中有一个登录屏幕和注册屏幕。我还创建了一个显示主页屏幕的 Tab 控制器。用户登录或注册后,我希望能够转到选项卡控制器并将其设为根视图控制器,因此用户无法返回登录或注册页面。请问这个怎么实现?

下面是用户登录时的几行代码

if statusCode == 200 
                do 
                    let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
                    let result = json as? [String: AnyObject]
                    guard let token = result!["token"], let firstname = result!["user"]!["firstname"]! else 
                        print("Error: \(error?.debugDescription)")
                        return
                    

                    NSUserDefaults.standardUserDefaults().setObject(token, forKey: "token")
                //code to segue to the tab controller from the nav controller (login screen)
                self.getHome()

                
                catch 
                    print("Error receiving data")
                
            
        )
        task.resume()



    func getHome() 
     let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let tableController = storyboard.instantiateViewControllerWithIdentifier("tabController") as? UITableViewController

        let appledDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appledDelegate.window?.rootViewController = tableController
 

在我的 AppDelegate.swift 文件中,如果没有调用登录页面或显示主页,我会检查是否有令牌:

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

    let obj = NSUserDefaults.standardUserDefaults().objectForKey("token")

    if obj == nil 

    let welcomeHomeController:UIViewController = WelcomeHomeController(nibName: nil, bundle: nil)
    welcomeHomeController.view.backgroundColor = UIColor.whiteColor()
    let navController:UINavigationController = UINavigationController(rootViewController: welcomeHomeController)

    //create window
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.backgroundColor = UIColor.darkGrayColor()
    self.window?.rootViewController = navController
    self.window!.makeKeyAndVisible()

    return true

    

【问题讨论】:

这篇文章可能对您有所帮助! ***.com/questions/39159444/… 【参考方案1】:
func getHome() 
 let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let navController = storyboard.instantiateViewControllerWithIdentifier("tabController") as? UINavigationController //be sure to set the "tabController" in the navigationController and not in the tabcontroller

    let next = navController.topViewController as! YourTabRootViewController
    self.navigationController.presentViewController(next, animated: true, completion: nil)

 

【讨论】:

以上是关于如何从导航控制器切换到选项卡控制器并使其成为根视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何识别从UITabBarController中的更多选项卡或单独选项卡单击视图控制器?

单击隐藏选项卡视图控制器中的后退按钮时如何显示选项卡

在每个选项卡错误处使用导航控制器从视图控制器转换到选项卡栏控制器

从导航控制器到选项卡控制器视图

以编程方式从模态视图控制器切换父选项卡

如何同时切换 tab 和 pushViewController?