【问题标题】:iOs - unrecognized selector sent to instance on TextActioniOs - 无法识别的选择器发送到 TextAction 上的实例
【发布时间】:2016-03-16 14:34:32
【问题描述】:

这个错误我有一些问题:

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

当我尝试创建警报时出现错误。这是代码:

@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)")
            }
        }
}

更新:当我点击我的 TextField 的“完成”时出现同样的错误。 + 我添加了 getUser 函数。

【问题讨论】:

  • 附注:按照惯例,您应该将函数名写成小写func search(sender ...
  • 她在我的代码中是小写的,只是在这里我不知道为什么。已编辑:)
  • 嘿@Makaille,您在 else 块中的警报代码非常完美,并且可以正常工作。

    我无法理解您的代码部分 response in if response.boolValue == true { /* ... */ } else { }
  • 你好,@AvinashJadhav getUser 将返回真或假,如果用户存在与否。如果我的用户存在,我会做一些事情,比如为其他视图准备数据等,如果不存在,我只想创建警报。是不是更清楚了?
  • response in if response.boolValue == true { } 正在制作单行代码语句并且看起来不正确。因为response in if { } 看起来是正确的。我认为它应该类似于语法object in objectArray,即循环执行。所以请确保同一行在语法上是正确的。

标签: ios iphone ios9 alert


【解决方案1】:

您可能在错误的线程中,因为您在一个块中显示警报,请尝试:

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

【讨论】:

  • 为什么要放入异步块?
  • @AvinashJadhav 在主线程而不是后台线程中执行
  • 没有理由强制它同步,重要的是它运行在主队列上
  • 嘿@simpleBob,他的警报代码没有任何问题。我在机器上试过。我认为问题可能属于他的getUser 方法调用,if 条件和响应处理。
  • @AvinashJadhav 如果问题确实是由线程引起的,您可能无法仅通过复制/粘贴他的代码来重现它,因为您的代码可能在正确的线程上运行
【解决方案2】:

处理程序 当用户选择操作时要执行的块。此块没有返回值,并将选定的操作对象作为其唯一参数。 您不在这里指定操作。 像这样:

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:

【讨论】:

  • handler是webservice请求的响应。
猜你喜欢
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 2019-08-28
相关资源
最近更新 更多