单击按钮验证并更改控制器视图iOS

时间:2018-09-10 01:52:35

标签: swift xcode swift4 xcode9 ibaction

我是Swift iOS开发的新手,正在创建一个用户登录页面,当他们单击Sign In按钮时,会对用户输入进行一些验证,如果成功,则将用户发送至一个新页面。在情节提要中,我有两个视图控制器,即登录页面和成功登录后的页面。我已经从登录页面上的Sign In按钮创建了一个segue到第二页。这是以下代码。

@IBAction func loginAction(_ sender: Any) {
    guard let username = usernameField.text, let password = passwordField.text else {
        return
    }
    if (username.isEmpty && password.isEmpty) {
        displayAlert(message: "username and password are not set")
        return
    }
    if (username.isEmpty) {
        displayAlert(message: "username is not set")
        return
    }
    if (password.isEmpty) {
        displayAlert(message: "password is not set")
        return
    }
}

此代码有效,并在登录后成功将用户引导到正确的页面。但是,当我添加提示"Success"的警报时,关闭警报后将不会加载新页面。这是添加成功登录警报的代码。在函数loginAction中,在最终条件之后立即添加了代码。

let alert = UIAlertController(title: "Success", message: "Sucessfully logged in", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)

为什么没有收到此警报将我发送到新页面的原因是什么?如果"Success"警报被注释掉,我将被发送到正确的页面。

我正在使用Xcode 9Swift 4

编辑:这是代码的类别。

class ViewController: UIViewController {
    @IBOutlet weak var usernameField: UITextField!
    @IBOutlet weak var passwordField: UITextField!

    @IBAction func loginAction(_ sender: Any) {
        guard let username = usernameField.text, let password = passwordField.text else {
            return
        }
        if (username.isEmpty && password.isEmpty) {
            displayAlert(message: "username and password are not set")
            return
        }
        if (username.isEmpty) {
            displayAlert(message: "username is not set")
            return
        }
        if (password.isEmpty) {
            displayAlert(message: "password is not set")
            return
        }
        login(info: username)
        // self.present(alert, animated: true, completion: nil)
        // self.present(self, animated: true) {
        //     let alert = UIAlertController(title: "Success", message: "Sucessfully logged in with \(username)", preferredStyle: UIAlertControllerStyle.alert)
        //     alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler: nil))
        //     self.present(alert, animated: true, completion: nil)
        // }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private func login(info: String) {
        let alert = UIAlertController(title: "Success", message: "Sucessfully logged in with \(info)", preferredStyle: UIAlertControllerStyle.alert)
        // alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default) { action in self.dismiss(animated: true) })
        alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }

    private func displayAlert(message: String) {
        let alert = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:0)

这是我需要做的才能使其正常工作。我更新了函数loginAction

let alert = UIAlertController(title: "Success", message: "Sucessfully logged in with \(info)", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default) { action in
    self.performSegue(withIdentifier: "loginSegue", sender: nil)
})
self.present(alert, animated: true, completion: nil)