2015-06-06 150 views
0

我有一個表格視圖控制器,一個滑動手勢識別器,只要用戶向上滑動就會觸發NSNotificationCenter.defaultCenter().postNotificationName("DuskTheme", object: nil)爲什麼我的NSNotificationCenter拋出異常?

在viewDidLoad中()函數,我有以下觀察者:NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)它調用改變所述元件的電流視圖控制器上的顏色(即主題)的函數dusk(notification: NSNotification)

我想改變我的導航欄的顏色,以及當用戶刷卡,所以我子類的navigationController並添加了以下觀測到其viewDidLoad中():NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)還有dusk(notification: NSNotification)功能包含了新的顏色我從Storyboard鏈接的導航欄。

這裏是我的自定義導航控制器類:

class customNavigationController: UINavigationController { 
    @IBOutlet weak var featuredNavBar = ThemeManager.navigationbar 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     //Adding a theme notification observer 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil) 

     func dusk(notification: NSNotification) { 
      UIView.animateWithDuration(1, animations: { 
       UIApplication.sharedApplication().statusBarStyle = .LightContent 
       self.featuredNavBar?.barTintColor = UIColor(red: 69/255, green: 69/255, blue: 69/255, alpha: 1) 

      }) 
     } 

    } 

} 

現在,出於某種原因,每當表視圖控制器刷卡應用程序引發以下異常:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TestApp.customNavigationController dusk:]: unrecognized selector sent to instance 0x7939c910' 

這是錯誤所引起的手勢識別器?在繼承導航控制器之前,它工作正常。更重要的是,檢測主題已更改並更改導航欄顏色的更好方法是什麼?

提前致謝!

+0

如果'黃昏()'沒有參數,將選擇器更改爲'黃昏',擺脫':'。 – vacawama

+0

@vacawama修正了上面的代碼 – cyril

+0

'func dusk(notification:NSNotification)''customNavigationController'內的頂層方法(即它不在另一個函數裏面,比如'viewDidLoad')? – vacawama

回答

2

移動dusk()viewDidLoad()之外。它需要在最高級別:

class customNavigationController: UINavigationController { 
    @IBOutlet weak var featuredNavBar = ThemeManager.navigationbar 

    func dusk(notification: NSNotification) { 
     UIView.animateWithDuration(1, animations: { 
      UIApplication.sharedApplication().statusBarStyle = .LightContent 
      self.featuredNavBar?.barTintColor = UIColor(red: 69/255, green: 69/255, blue: 69/255, alpha: 1) 

     }) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     //Adding a theme notification observer 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil) 
    } 
} 
+0

它是如何把它放在頂部固定它?訂單如何影響應用程序的運行方式?我對這一切仍然陌生並且自學成才,你能介意給我解釋一下嗎? – cyril

+2

如果'dusk()'高於或低於'viewDidLoad',它並不重要,但它需要與'viewDidLoad'具有相同的嵌套級別。該選擇器在'customNavigatorController'實例上調用,因此該函數需要是頂層函數,而不是嵌套在另一個函數內。 – vacawama

相關問題