2015-10-06 25 views
0

部署目標:8.4 夫特:2.0+ [UIView的onBackgroundRequest:]:無法識別的選擇發送到類

我有具有的UITableView的UIView,因爲它是tableHeaderView UIViewController類。 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)觸發後一butto n輕觸在StartTableHeaderView並分派UINotification,我有以下錯誤擊球:

2015-10-06 17:31:00.738 Elmo[1838:55864] +[UIView onBackgroundRequest:]: unrecognized selector sent to class 0x3272d90 2015-10-06 17:31:00.745 Elmo[1838:55864] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIView onBackgroundRequest:]: unrecognized selector sent to class 0x3272d90'

+1

你需要'removeObserver'當對象重新分配 –

+0

我有這個在我的課,雖然: 'DEINIT { NSNotificationCenter.defaultCenter()removeObserver(個體經營)。 }' –

+0

在您似乎試圖調用onBackgroundRequest UIView類的方法,並有一個在你發佈的代碼...... –

回答

1

如何調用onBackgroundRequest:設立StartTableHeaderView類上的按鈕的動作?您是使用Interface Builder還是使用代碼? 如果在代碼中,你可以發佈嗎?

我想你可能不會像你想象的那樣發佈通知。在StartTableHeaderViewonBackgroundRequest:內部放置一個斷點,看看它是否被觸發。

錯誤是告訴你,onBackgroundRequest:沒有找到,不onBackgroundRequested:

+0

不知怎的,XIB和'StartTableHeaderView'的類文件之間的鏈接無法正常工作。雖然它設法顯示而不會拋出錯誤,但是任何IB連接都會變得致命。我需要刪除XIB和類文件,因爲我找不到任何可能的突破點。它的工作現在很好。謝謝史蒂夫! –

相關問題