+ [UIView onBackgroundRequest:]:发送给类的无法识别的选择器

时间:2015-10-06 12:19:06

标签: ios swift

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

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

我的UIViewController类非常简单。

StartViewController UIViewController ):

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])
    {
         ...
    }
}

StartTableHeaderView UIView ):

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] ***终止app由于   未捕获的异常' NSInvalidArgumentException',原因:' + [UIView   onBackgroundRequest:]:无法识别的选择器发送到类0x3272d90'

1 个答案:

答案 0 :(得分:1)

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

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

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