2016-03-16 51 views
1

我一些問題,與此錯誤:IOS - 無法識別的選擇發送到實例上TextAction的

ProjectName.ViewController textAction:]: unrecognized selector sent to instance 0x7fa60bc3840 

當我嘗試創建一個alerte出現錯誤。下面是代碼:

@IBAction func trySearch(sender: UIButton) { 
    self.api.getUser(self.textField.text!) { isResponse in 
     if (isResponse.count == 0) { 
//Still crash HERE. 
      let alert = UIAlertController(title: "Error", message: "This username doesn't exist", preferredStyle: UIAlertControllerStyle.Alert) 
       alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
      self.presentViewController(alert, animated: true, completion: nil) 
     } 
     else { 
      print("existing username") 
     } 
    } 

如果我評論的所有報警代碼,並通過一個簡單的打印它的作品取代它......我真的不明白爲什麼...感謝的!

的getUser功能:

func getUser(user: String, completion: ((isReponse: AnyObject) -> Void)) { 

    let request = NSMutableURLRequest(URL: NSURL(string: "https://********")!) 
    request.HTTPMethod = "GET" 
    request.setValue("Bearer \(self.loadToken())", forHTTPHeaderField: "Authorization") 

    Alamofire.request(request) 
     .responseJSON { response in 
      switch response.result { 
      case .Success(let JSON): 
       completion(isReponse: JSON) 
      case .Failure(let error): 
       print("Request failed with error: \(error)") 
      } 
     } 
} 

UPDATE:當我點擊我的文本字段的 「完成」 同樣的錯誤。 +我添加getUser函數。

+0

只是一個側面說明:按照慣例,你應該在較低的情況下寫的函數名'FUNC搜索(發件人...' – Daniel

+0

她是在較低的情況下,在我的代碼,只是在這裏我不知道爲什麼編輯:) – Makaille

+0

嘿@Makaille,你的代碼在其他塊的警報是完美的,它沒有任何問題的作品。

我無法理解你的代碼的一部分 '應答如果response.boolValue ==真{ /* ... * /} 其他 {}' –

回答

0

你可能會在錯誤的線程,因爲你是一個塊中呈現的警報,請嘗試:

dispatch_async(dispatch_get_main_queue(), { _ in 
    let alert = UIAlertController(title: "Error", message: "This username doesn't exist", preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
    self.presentViewController(alert, animated: true, completion: nil) 
}) 
+0

爲什麼要放入異步塊? –

+0

@AvinashJadhav在主線程而不是後臺線程中執行它 – redent84

+0

沒有理由強制它是同步的,重要的部分是它在主隊列上運行 – Daniel

-1

處理 當用戶選擇的動作執行塊。該塊沒有返回值,並將選定的操作對象作爲其唯一參數。 你不要在這裏指定動作。 讓這樣的:

UIAlertAction* ok = [UIAlertAction 
       actionWithTitle:@"OK" 
       style:UIAlertActionStyleDefault 
       handler:^(UIAlertAction * action) 
       { 
        //Do some thing here 
        [view dismissViewControllerAnimated:YES completion:nil]; 

       }]; 
// add action to your alertController 

[alert addAction:ok]; 

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertAction_Class/#//apple_ref/occ/clm/UIAlertAction/actionWithTitle:style:handler

+0

處理程序是web服務請求的響應。 – redent84

相關問題