web-dev-qa-db-fra.com

Swift: comment afficher un contrôleur de barre d'onglets après une vue de connexion

J'ai vu de nombreux articles similaires à celui-ci ici, mais ils concernent tous Objective-C pendant que je développe mon application dans Swift. Comme vous pouvez le voir sur l'image, j'ai une vue d'écran de connexion et j'ai correctement mis en œuvre le mécanisme de connexion.

enter image description here

Maintenant, j'aimerais que lorsque la connexion a réussi, le contrôleur de la barre d'onglets s'affiche. Dans mon contrôleur de vue de connexion, j'ai cette fonction pour la connexion:

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

Je pense donc que je devrais montrer le contrôleur de la barre d'onglets après

NSLog("Login OK")

mais je ne sais pas comment. Je suis un débutant de Swift/XCode ... si vous pouvez m'expliquer. Merci à tous ceux qui ont lu.

20
SagittariusA

Pour afficher le contrôleur de la barre d'onglets à partir de la page de connexion, connectez la page de connexion et TabbarController avec une séquence Show et attribuez-lui un identificateur dans l'inspecteur d'attributs (dites "mySegueIdentifier").

Pour ajouter un enchaînement, faites un clic droit et faites-le glisser depuis le contrôleur de vue de connexion vers TabbarController.

En cas de connexion réussie, vous pouvez simplement appeler la méthode "performSegueWithIdentifier" comme suit

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

Dans votre cas, vous l'appelez après cette ligne.

NSLog("Login OK")

Si vous ne souhaitez pas naviguer de la page de connexion à TabbarController, vous pouvez également le définir en tant que rootViewController après une connexion réussie. Pour ce faire, définissez un identifiant sur TabbarController (dites "myTabbarController")

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

 var initialViewController = self.storyboard!.instantiateViewControllerWithIdentifier("myTabbarControllerID") as! UIViewController
 appDelegate.window?.rootViewController = initialViewController
 appDelegate.window?.makeKeyAndVisible()

Modifier:

Swift 3

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

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

Bon codage .. :)

45
Dev

J'ai rencontré ce même problème en essayant de passer d'un contrôleur que j'ai utilisé pour touchID à un TabBarController. En effectuant la transition dans un bloc asynchrone, j'ai résolu le problème.

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

Donnez à votre contrôleur de barre d'onglets un StoryboardID (dites "tabbar") et poussez-le comme un UIViewController normal:

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

self.navigationController?.pushViewController(nextViewController, animated: true)
2
S.S.D
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)
}
1
Kristijan Delivuk

Appelez le Tabbarcontroller depuis n'importe quel contrôleur Par code ci-dessous, le premier élément par défaut est sélectionné

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

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

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