【发布时间】:2016-01-03 08:39:38
【问题描述】:
部署目标:8.4 斯威夫特:2.0
我有一个 UIViewController 类,它有一个 UITableView 和一个 UIView,因为它是 tableHeaderView。 UIViewController 类有一个 UINotification 监听器,作为tableHedearView 的 UIView dispatches 一个 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] *** 由于以下原因而终止应用程序 未捕获的异常 'NSInvalidArgumentException',原因:'+[UIView onBackgroundRequest:]: 无法识别的选择器发送到类 0x3272d90'
【问题讨论】:
-
对象释放时需要
removeObserver -
我的课上有这个,不过:
deinit { NSNotificationCenter.defaultCenter().removeObserver(self); } -
在异常中,您似乎正在尝试调用 onBackgroundRequest UIView 类方法,而您发布的代码中没有此类方法...
-
那是我不确定的 - 观察者被添加到
self这是 UIViewController,我在那里有onBackgroundRequest方法(正如你在我的源代码中看到的那样),然后它是如何尝试的在 UIView 中查看方法! -
您的 UIViewController 中没有 onBackgroundRequest 方法。有 onBackgroundRequested 方法