无法使用参数列表类型 '(title:string, message:
cannot invoke initializer for type 'UIAlertController' with an argument list type '(title:string, message:
更新到 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)
})
}
})
}
}
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)
使用带有自定义 UIAlertAction
设计的 UIAlertViewController
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)
您可以在 Swift Cecil Costa 的 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)
}
}
更新到 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)
})
}
})
}
}
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)
使用带有自定义 UIAlertAction
UIAlertViewController
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)
您可以在 Swift Cecil Costa 的 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)
}
}