控制台警告呈现新的视图控制器

时间:2016-12-13 08:01:18

标签: ios swift uialertcontroller

@IBAction func signinButton(_ sender: AnyObject) {
        self.ActivityIndicator.startAnimating()

        let id   = usernameTextField.text;
        let pwd  = passwordTextField.text;

        if (id!.isEmpty || (pwd!.isEmpty)){
            let alert = UIAlertController(title: "Information!", message: "Username and Password is not empety", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.destructive, handler: nil))
            present(alert, animated: true, completion: nil)
            self.present(alert, animated: true, completion: nil)
        }
}

此错误: 警告:尝试显示其视图不在窗口层次结构中!

请帮帮我..

3 个答案:

答案 0 :(得分:2)

试试这个(只删除最后一个self.present行):

@IBAction func signinButton(_ sender: AnyObject) {
        self.ActivityIndicator.startAnimating()

        let id   = usernameTextField.text;
        let pwd  = passwordTextField.text;

        if (id!.isEmpty || (pwd!.isEmpty)){
            let alert = UIAlertController(title: "Information!", message: "Username and Password is not empety", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.destructive, handler: nil))
            present(alert, animated: true, completion: nil)
        }
}

答案 1 :(得分:1)

您有2个present个功能。所以只需更改您的代码:

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

为:

present(alert, animated: true, completion: nil)

答案 2 :(得分:0)

如何制作通知徽章。 如果在表格中重新加载时有新数据,则会像这样在tableview上显示通知标记。 enter image description here