登录后,打开一个新的视图控制器swift 2.0

时间:2016-03-14 22:16:44

标签: swift login pushviewcontroller

如果登录成功,我正在尝试更改视图控制器,但我不确定如何执行此操作。这是我到目前为止所尝试的。提前谢谢!

@IBAction func signinaction(sender: AnyObject) {
    let user = self.usernamefield.text!

    ref.authUser(emailfield.text, password: passwordfield.text, withCompletionBlock: { error, authData in

        if error != nil
        {
            let alert = UIAlertController(title: "Error", message: "Enter Email and Password.", preferredStyle: UIAlertControllerStyle.Alert)
            let action = UIAlertAction(title: "Ok", style: .Default, handler: nil)
            alert.addAction(action)
            self.presentViewController(alert, animated: true, completion: nil)
            print("can not sign in")
        }
        else
        {

            let storyboard = UIStoryboard(name: "Main", bundle: nil);
            let viewName:NSString = "NewView"
            let vc = storyboard.instantiateViewControllerWithIdentifier(viewName as String) as! HomeViewController
            let uid = authData.uid
            print("Success with user: \(uid)")
            let alert = UIAlertController(title: "Success", message: "Welcome \(user)", preferredStyle: UIAlertControllerStyle.Alert)
            let action = UIAlertAction(title: "Ok", style: .Default, handler: nil)
            alert.addAction(action)
            self.navigationController?.pushViewController(vc as HomeViewController, animated: true)

        }

    })
}

2 个答案:

答案 0 :(得分:0)

假设所有这些视图控制器都包含在导航控制器中,那么你所拥有的应该可以正常工作。但请注意,您正在创建一个从未显示的提醒。我的猜测是你要显示成功警报,然后打开新的视图控制器,如:

            let alert = UIAlertController(title: "Success", message: "Welcome \(user)", preferredStyle: UIAlertControllerStyle.Alert)
            let action = UIAlertAction(title: "Ok", style: .Default) { _ in
                self.navigationController?.pushViewController(vc, animated: true)
            }
            alert.addAction(action)

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

如果仍然无法正常工作,我确保当前的视图控制器实际显示在导航控制器中。

答案 1 :(得分:0)

看起来您只需要在警报操作中添加导航代码。目前,您将handler参数设置为nil

maxClientCnxns=50
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/var/lib/zookeeper
# the port at which the clients will connect
clientPort=2181
# the directory where the transaction logs are stored.
dataLogDir=/var/lib/zookeeper

变成这个

let action = UIAlertAction(title: "Ok", style: .Default, handler: nil)