Swift:如何在登录视图后显示标签栏控制器

Sag*_*usA 21 xcode uitabbarcontroller ios swift

我在这里看过很多与此相似的帖子,但是当我在Swift中开发我的应用程序时,它们都是关于Objective-C的.从图像中我可以看到我有一个登录屏幕视图,我正确实现了登录机制.

在此输入图像描述

现在我希望在登录成功后,显示标签栏控制器.在我的登录视图控制器中,我有此功能用于登录:

var finalURL:NSString = "\(Settings.webServerLoginURL)?username=\(username)&password=\(password)"

        LoginService.requestLoginWithURL(NSURL(string: finalURL as String)!, completionHandler: { (success) -> Void in
            if (success) {
                NSLog("Login OK")

                /* Scarica dal database i tasks di LoggedUser.id */
                /* Redirect al tab HOME dell'applicazione dove si mostrano il numero di task
                di quell'utente ed in cima "BENVENUTO: name surname" */


            }

            else {
                self.alertView.title = "Autenticazione fallita!"
                self.alertView.message = "Username o passowrd."
                self.alertView.delegate = self
                self.alertView.addButtonWithTitle("OK")
                self.alertView.show()
            }
Run Code Online (Sandbox Code Playgroud)

所以我认为我之后应该显示标签栏控制器

NSLog("Login OK")
Run Code Online (Sandbox Code Playgroud)

但我不知道怎么做.我是Swift/XCode的初学者...如果你能解释我的话.感谢所有阅读过的人.

Dev*_*Dev 48

要从"登录"页面显示选项卡栏控制器,请使用"显示segue"连接"登录"页面和TabbarController,并在属性检查器(Say"mySegueIdentifier")中为其指定一个标识符.

要添加segue,只需右键单击并从Login视图控制器拖动到TabbarController.

在成功登录后,您只需调用"performSegueWithIdentifier"方法,如下所示

self.performSegueWithIdentifier("mySegueIdentifier", sender: nil)
Run Code Online (Sandbox Code Playgroud)

在你的情况下,你在这一行之后调用它.

NSLog("Login OK")
Run Code Online (Sandbox Code Playgroud)

如果您不想从"登录"页面导航到TabbarController,则可以在成功登录后将其设置为rootViewController.为此,请将标识符设置为TabbarController(说"myTabbarController")

 let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate

 var initialViewController = self.storyboard!.instantiateViewControllerWithIdentifier("myTabbarControllerID") as! UIViewController
 appDelegate.window?.rootViewController = initialViewController
 appDelegate.window?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)

编辑:

斯威夫特3

 let appDelegate = UIApplication.shared.delegate! as! AppDelegate

 let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID") 
 appDelegate.window?.rootViewController = initialViewController
 appDelegate.window?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)

快乐的编码.. :)

  • 我编辑了我的答案.检查一次.在您的情况下,您必须在登录视图控制器和标签栏控制器之间给出segue.不在登录按钮和标签栏控制器之间. (3认同)