alertView现在是alertController

时间:2016-05-22 17:19:17

标签: ios swift uialertcontroller

我正在尝试在iOS中完成我的登录页面,并且在我将alertView更改为alertController之后,它说“类型'UIAlertController'的值没有成员'show'”。

我的代码是:

if ( username.isEqualToString("") || password.isEqualToString("") ) {

        var alertView:UIAlertController = UIAlertController()
        alertView.title = "Sign Up Failed!"
        alertView.message = "Please enter Username and Password"
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
    } else if ( !password.isEqual(confirm_password) ) {

        var alertView:UIAlertController = UIAlertController()
        alertView.title = "Sign Up Failed!"
        alertView.message = "Passwords doesn't Match"
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
    } else {

我得到了错误:

  

'UIAlertController'没有成员'show'“和'UIAlertController'类型的值没有成员'委托'”等。

此外,由于我将其更改为alertController,这部分代码也出现了错误:

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:reponseError)

    if ( urlData != nil ) {
        let res = response as! NSHTTPURLResponse!;

        NSLog("Response code: %ld", res.statusCode);

        if (res.statusCode >= 200 && res.statusCode < 300)
        {
            var responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

            NSLog("Response ==> %@", responseData);

            var error: NSError?

            let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary

在调用中声明“额外参数'错误'但是如果我把错误拿出来,那么它只是给了我一个不同的错误,希望我带走)并连续添加一个!

谢谢!

3 个答案:

答案 0 :(得分:2)

UIAlertViewUIAlertController完全不同,可互换

UIAlertController的等效代码是

let alertController = UIAlertController(title: "Sign Up Failed!", 
                                      message: "Please enter Username and Password", 
                               preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)

要解决extra argument error,您需要Swift 2的新错误处理语法

do {
   let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options:.MutableContainers) as! [String:AnyObject]
} catch {
    print(error)
}

如果jsonData不能保证是字典(最好使用Swift原生类型),则需要额外的行来检查具有可选绑定的类型

do {
   let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options:.MutableContainers)
   if jsonDict = jsonData as? [String:AnyObject] {
      // do something with the dictionary
   }
} catch {
    print(error)
}

PS:username.isEqualToString("")的更简单的语法是username.isEmpty

答案 1 :(得分:0)

你必须像UIViewController一样呈现它:

self.presentViewController(alertView, animated: true, completion: nil)

答案 2 :(得分:0)

如果创建没有任何操作按钮的警报框,请尝试以下代码。

df.unpersist()

如果您创建带有操作按钮的警报框,请尝试以下代码。

let alertController = UIAlertController(title: "Alert", message: "This is an alert.", preferredStyle: .alert)


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