Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 单击按钮并更改控制器视图时的验证_Swift_Xcode_Swift4_Xcode9_Ibaction - Fatal编程技术网

Swift 单击按钮并更改控制器视图时的验证

Swift 单击按钮并更改控制器视图时的验证,swift,xcode,swift4,xcode9,ibaction,Swift,Xcode,Swift4,Xcode9,Ibaction,我是Swift iOS开发的新手,我正在创建一个用户登录页面,当他们单击登录按钮时,会对用户输入执行一些验证,如果成功,会将用户发送到新页面。在storyboard中,我有两个视图控制器:登录页面和成功登录后的页面。我已经创建了一个从登录页面上的登录按钮到第二页的序列。下面是下面的代码 @IBAction func loginAction(_ sender: Any) { guard let username = usernameField.text, let password = pa

我是Swift iOS开发的新手,我正在创建一个用户登录页面,当他们单击
登录
按钮时,会对用户输入执行一些验证,如果成功,会将用户发送到新页面。在storyboard中,我有两个视图控制器:登录页面和成功登录后的页面。我已经创建了一个从登录页面上的
登录
按钮到第二页的序列。下面是下面的代码

@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)
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)
我没有被发送到带有此警报的新页面的原因是什么?如果
“Success”
警报被注释掉,我将被发送到正确的页面

我使用的是
Xcode 9
Swift 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)
    }
}

这是我需要做的,以使它正常工作。我更新了我的函数
登录操作

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

这是因为你不会等到用户选择一个动作。使用完成闭包。我用完成闭包做了类似的事情,但什么都没有。谢谢你告诉我使用闭包,但是如何像以前一样正确地关闭和加载页面?那会有帮助的<代码>alert.addAction(UIAlertAction(标题:“完成”,样式:UIAlertActionStyle.default){self.disease中的操作(动画:true)}