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

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

Swift: how to show a tab bar controller after a login view

iosxcodeswiftuitabbarcontroller

提问by SagittariusA

I have seen many posts similar to this here but they are all about Objective-C while I am developing my app in Swift. As you can see from the image I have a login screen view and I correctly implemented the login mechanism.

我在这里看到过很多与此类似的帖子,但它们都是关于 Objective-C 的,而我正在 Swift 中开发我的应用程序。正如您从图像中看到的,我有一个登录屏幕视图,并且我正确地实现了登录机制。

enter image description here

enter image description here

Now I would like that when the login has succedeed, then the tab bar controller is showed. In my login view controller I have this function for login:

现在我希望在登录成功后显示标签栏控制器。在我的登录视图控制器中,我有这个登录功能:

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

So I think that I should show tab bar controller after

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

NSLog("Login OK")

but I don't know how. I am a beginner of Swift/XCode...if you can explain me. Thanks to all who have read.

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

回答by Dev

To show tab bar controller from Login page, connect the Login page and TabbarController with a Show segue and give it an identifier in attributes inspector (Say "mySegueIdentifier").

要从登录页面显示标签栏控制器,请将登录页面和 TabbarController 与 Show segue 连接起来,并在属性检查器中为其指定一个标识符(比如“mySegueIdentifier”)。

To add segue, just right click and drag from Login view controller to TabbarController.

要添加转场,只需右键单击并从登录视图控制器拖动到 TabbarController。

In successful Login you can simply call "performSegueWithIdentifier" method as follows

在成功登录后,您可以简单地调用“performSegueWithIdentifier”方法如下

self.performSegueWithIdentifier("mySegueIdentifier", sender: nil)

In your case you call it after this line.

在您的情况下,您在此行之后调用它。

NSLog("Login OK")

If you don't want to navigate from Login page to TabbarController, you can also set it as rootViewController after successful Login. To do this, set an identifier to TabbarController (Say "myTabbarController")

如果您不想从登录页面导航到 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()

Edit:

编辑:

Swift 3

斯威夫特 3

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

 let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID") 
 appDelegate.window?.rootViewController = initialViewController
 appDelegate.window?.makeKeyAndVisible()

Happy coding.. :)

快乐编码.. :)

回答by Brian

I ran into this same issue when trying to segue from a controller I used for touchID to a TabBarController. By making the segue in an async block I resolved the issue.

我在尝试从用于 touchID 的控制器转到 TabBarController 时遇到了同样的问题。通过在异步块中进行转场,我解决了这个问题。

dispatch_async(dispatch_get_main_queue(), {
    self.dismissViewControllerAnimated(false, completion: {})
    self.performSegueWithIdentifier("authnToAppSegue", sender: nil)
})

回答by S.S.D

Give your tab bar controller a StoryboardID (say "tabbar") and push it just like a normal UIViewController:

给你的标签栏控制器一个 StoryboardID(比如“tabbar”)并像普通的 UIViewController 一样推送它:

let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as! UIViewController

self.navigationController?.pushViewController(nextViewController, animated: true)

回答by Kristijan Delivuk

func setTabBarVisible(visible:Bool, animated:Bool) {

//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time

    // bail if the current state matches the desired state
    if (tabBarIsVisible() == visible) { return }

    // get a frame calculation ready
    let frame = self.tabBarController?.tabBar.frame
    let height = frame?.size.height
    let offsetY = (visible ? -height! : height)

    // zero duration means no animation
    let duration:NSTimeInterval = (animated ? 0.3 : 0.0)

    //  animate the tabBar
    if frame != nil {
        UIView.animateWithDuration(duration) {
            self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!)
            return
        }
    }
}

func tabBarIsVisible() ->Bool {
    return self.tabBarController?.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame)
}

// Call the function from tap gesture recognizer added to your view (or button)

@IBAction func tapped(sender: AnyObject) {
    setTabBarVisible(!tabBarIsVisible(), animated: true)
}

回答by Upendranath Reddy

Call the Tabbarcontroller from any controller By below Code, default first item is selected

从任何控制器调用 Tabbarcontroller 通过下面的代码,默认选择第一项

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "HomeTabBar") as! UITabBarController

    self.navigationController?.pushViewController(nextViewController, animated: true)