登录警报swift 2.0后打开新的视图控制器
如果登录成功,我将尝试更改视图控制器,但我不确定如何执行此操作。这就是我迄今为止所尝试的。提前谢谢登录警报swift 2.0后打开新的视图控制器,swift,login,pushviewcontroller,Swift,Login,Pushviewcontroller,如果登录成功,我将尝试更改视图控制器,但我不确定如何执行此操作。这就是我迄今为止所尝试的。提前谢谢 @IBAction func signinaction(sender: AnyObject) { let user = self.usernamefield.text! ref.authUser(emailfield.text, password: passwordfield.text, withCompletionBlock: { error, authData in
@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)
}
})
}
假设所有这些都包含在一个导航控制器中的视图控制器中,您所拥有的应该可以正常工作。但是请注意,您正在创建一个从未显示的警报。我猜您希望显示成功警报,然后打开新的视图控制器,如:
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)
如果这仍然不起作用,我会确保当前的视图控制器实际显示在导航控制器中。假设所有这些都包含在导航控制器中的视图控制器中,您所拥有的应该可以正常工作。但是请注意,您正在创建一个从未显示的警报。我猜您希望显示成功警报,然后打开新的视图控制器,如:
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)
如果这仍然不起作用,我会确保当前视图控制器实际显示在导航控制器中。看起来您只需要在警报操作中添加导航代码。当前,处理程序参数设置为nil 这个 变成这样
let alertAction = UIAlertAction(title: "Ok", style: .Default) { (action) -> Void in
self.navigationController?.pushViewController(vc as HomeViewController, animated: true)
}
看起来您只需要在警报操作中添加导航代码。当前,处理程序参数设置为nil 这个 变成这样
let alertAction = UIAlertAction(title: "Ok", style: .Default) { (action) -> Void in
self.navigationController?.pushViewController(vc as HomeViewController, animated: true)
}