-1

更新到 Swift2 后我收到此错误,不确定我缺少什么。这是我的代码:任何具有 var alert = UIAlertController 的东西都会发生错误,感谢您的帮助。

谢谢

 // Validate the text fields
    if username!.characters.count < 5 {
        var alert = UIAlertController(title: "Invalid", message: "Username must be greater than 5 characters", delegate: self, cancelButtonTitle: "OK")
        alert.show()

    } else if password!.characters.count < 8 {
        var alert = UIAlertController(title: "Invalid", message: "Password must be greater than 8 characters", delegate: self, cancelButtonTitle: "OK")
        alert.show()

    } else if email!.characters.count < 8 {
        var alert = UIAlertController(title: "Invalid", message: "Please enter a valid email address", delegate: self, cancelButtonTitle: "OK")
        alert.show()

    } else {
        // Run a spinner to show a task in progress
        var spinner: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView
        spinner.startAnimating()

        var newUser = PFUser()

        newUser.username = username
        newUser.password = password
        newUser.email = finalEmail

        // Sign up the user asynchronously
        newUser.signUpInBackgroundWithBlock({ (succeed, error) -> Void in

            // Stop the spinner
            spinner.stopAnimating()
            if ((error) != nil) {
                var alert = UIAlertController(title: "Error", message: "\(error)", delegate: self, cancelButtonTitle: "OK")
                alert.show()

            } else {
                var alert = UIAlertController(title: "Success", message: "Signed Up", delegate: self, cancelButtonTitle: "OK")
                alert.show()
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Home") 
                    self.presentViewController(viewController, animated: true, completion: nil)
                })
            }
        })
    }
}
4

3 回答 3

1

的初始化程序UIAlertController如下所示:

var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)

UIAlertController如下所示:

viewController.presentViewController(alert, animated: true, completion: { () -> Void in
    //something when the alert has been clicked
})

可选项是这样展开的:

guard let unWrappedUserName = username else {
    return
}

if unWrappedUserName.characters.count < 5 {

您可能希望像这样向警报添加操作:

var cancelAction = UIAlertAction(title: "Alert Option 1", style: UIAlertActionStyle.Cancel, handler: { (action) -> Void in
    // some stuff when they cancel
})

var stuffAction = UIAlertAction(title: "Alert Option 2", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
    // some other stuff
})

myAlertController.addAction(cancelAction)
myAlertController.addAction(stuffAction)
于 2015-11-09T20:52:50.440 回答
0

使用UIAlertViewController定制的设计UIAlertAction

let alert = UIAlertController(title: "Success", message: "Signed Up", preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "OK", style: .Cancel) { action in
        //Do whatever you want
}
alert.addAction(cancelAction)

self.presentViewController(alert, animated: true, completion: nil)
于 2015-11-09T21:03:27.867 回答
0

您可以在 Cecil Costa 的 Swift 2 Blueprints 中找到非常简洁的示例,其中的子章节:创建助手

extension UIViewController {
    func displayError(message:String){
        let alertController = UIAlertController(title: "Error", message: message, preferredStyle: .Alert)
        let alertAction = UIAlertAction(title: "Dismiss", style: .Cancel) { _ in
            self.dismissViewControllerAnimated(true, completion:nil)
        }
        alertController.addAction(alertAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}
于 2015-11-10T14:36:24.370 回答