2015-11-09 24 views
-1

)我得到此錯誤後更新到Swift2不知道我缺少什麼。這是我的代碼:錯誤發生在任何已VAR警報= UIAlertController任何幫助表示讚賞無法爲類型'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) 
       }) 
      } 
     }) 
    } 
} 
+0

不使用所有這些標籤。它或者與xcode或者語言相關,很少都是。它也僅僅是語言的一個版本,不是全部。 –

回答

0

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

我看到他們在那裏做了什麼。謝謝Menke – Birdman3000

+0

@ Birdman3000更新了您在10分鐘內要問的問題的答案。不要忘記註冊並歡迎來到SO –

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

您可以通過塞西爾科斯塔找到斯威夫特2個藍圖漂亮整潔的例子,裏面小節:創建助手

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) 
    } 
} 
相關問題