为什么我的代码不会进入下一个视图

时间:2017-08-17 13:36:09

标签: ios swift

调试时没有错误。接受登录后代码不会进入下一页...帮助请

if (emailValue == userEmail || passValue == userPassword) {

    //store user information in the app to maintain session

    let defaults = UserDefaults.standard

    defaults.set(usernameValue, forKey: "usernameValue")
    defaults.set(nameValue, forKey: "nameValue")
    defaults.set(emailValue, forKey: "emailValue")

    // Display Alert Message with confirmation
    let myAlert = UIAlertController(title: "Success!", message: Constants.SucessMessages.EIRP, preferredStyle: UIAlertControllerStyle.alert);
    let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default){ action in
        self.dismiss(animated: true, completion: nil);
        if let navController = self.navigationController {
            navController.popViewController(animated: true)
        }
    }
    myAlert.addAction(okAction);
    OperationQueue.main.addOperation {
        self.present(myAlert, animated: true, completion: nil)
    }

}

1 个答案:

答案 0 :(得分:3)

您想转到下一个屏幕,但您使用的代码是用于点击okAction的前一个屏幕

假设您的目标控制器的名称是DestinationViewController并且使用的ID是DestinationVCID,则可以使用以下选项转到下一个屏幕,具体取决于您当前的使用情况。

代码必须用于您使用的UIAlertAction,即okAction

<强>推

if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationVCID") as? DestinationViewController {

        if let navigator = navigationController {
            navigator.pushViewController(viewController, animated: true)
        }
}

<强>当前

if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationVCID") as? DestinationViewController
{
     present(vc, animated: true, completion: nil)
}