【问题标题】:+[UIView onBackgroundRequest:]: unrecognized selector sent to class+[UIView onBackgroundRequest:]: 发送到类的无法识别的选择器
【发布时间】:2016-01-03 08:39:38
【问题描述】:

部署目标:8.4 斯威夫特:2.0

我有一个 UIViewController 类,它有一个 UITableView 和一个 UIView,因为它是 tableHeaderView。 UIViewController 类有一个 UINotification 监听器,作为tableHedearView 的 UIView dispatches 一个 UINotification。

我的 UIViewController 类相当简单。

StartViewControllerUIViewController):

class StartViewController: UIViewController, UINavigationControllerDelegate
{
    @IBOutlet weak var tblContents: UITableView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // table header view
        let headerView = StartTableHeaderView(frame: CGRectMake(0, 0, tblContents.bounds.width, self.view.frame.height*0.3))
        tblContents.tableHeaderView = headerView

        // adds notification listener
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "onBackgroundRequested:", name: "changeBackground", object: nil)
    }

    func onBackgroundRequested(notification:NSNotification)
    {
        let uiImagePicker = UIImagePickerController()
        uiImagePicker.delegate = self
        uiImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

        self.presentViewController(uiImagePicker, animated: true, completion: nil)
    }
}

extension StartViewController: UIImagePickerControllerDelegate
{
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
         ...
    }
}

StartTableHeaderViewUIView):

class StartTableHeaderView: UIView
{
    ...

    @IBAction func onBackgroundRequest(sender: AnyObject)
    {
        NSNotificationCenter.defaultCenter().postNotificationName("changeBackground", object: nil)
    }
}

在运行时,每当方法 (onBackgroundRequest) 触发 StartTableHeaderView 中的按钮点击并调度 UINotification 时,我都会遇到以下错误:

2015-10-06 17:31:00.738 Elmo[1838:55864] +[UIView onBackgroundRequest:]: 发送到类 0x3272d90 的无法识别的选择器 2015-10-06 17:31:00.745 Elmo[1838:55864] *** 由于以下原因而终止应用程序 未捕获的异常 'NSInvalidArgumentException',原因:'+[UIView onBackgroundRequest:]: 无法识别的选择器发送到类 0x3272d90'

【问题讨论】:

  • 对象释放时需要removeObserver
  • 我的课上有这个,不过:deinit { NSNotificationCenter.defaultCenter().removeObserver(self); }
  • 在异常中,您似乎正在尝试调用 onBackgroundRequest UIView 类方法,而您发布的代码中没有此类方法...
  • 那是我不确定的 - 观察者被添加到 self 这是 UIViewController,我在那里有 onBackgroundRequest 方法(正如你在我的源代码中看到的那样),然后它是如何尝试的在 UIView 中查看方法!
  • 您的 UIViewController 中没有 onBackgroundRequest 方法。有 onBackgroundRequested 方法

标签: ios swift


【解决方案1】:

在类StartTableHeaderView 中调用onBackgroundRequest: 的按钮上的操作是如何设置的?您是在使用 Interface Builder 还是在代码中进行操作? 如果在代码中,您可以发布吗?

我认为您可能没有像您想的那样发布通知。在StartTableHeaderView onBackgroundRequest: 中放置一个断点,看看它是否被触发过。

错误告诉你找不到onBackgroundRequest:,而不是onBackgroundRequested:

【讨论】:

  • XIB 和StartTableHeaderView 的类文件之间的链接不知何故无法正常工作。尽管它设法在没有引发错误的情况下显示,但与该类的任何 IB 连接都变得致命。我需要删除 XIB 和类文件,因为我找不到任何可能的断点。它现在工作正常。谢谢史蒂夫!
猜你喜欢
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多