gpt4 book ai didi

ios - 为什么我的 NSNotificationCenter 抛出异常?

转载 作者:行者123 更新时间:2023-11-28 13:12:47 24 4
gpt4 key购买 nike

我有一个带有滑动手势识别器的 TableView Controller ,它会在用户向上滑动时触发 NSNotificationCenter.defaultCenter().postNotificationName("DuskTheme", object: nil)

在 viewDidLoad() 函数中,我有以下观察者:NSNotificationCenter.defaultCenter().addObserver(self, selector: "dusk:", name:"DuskTheme", object: nil)它调用函数 dusk(notification: NSNotification) 改变当前 View Controller (即主题)上元素的颜色。

我想在用户滑动时更改导航栏的颜色,所以我将 navigationController 子类化并将以下观察器添加到它的 viewDidLoad() 中:NSNotificationCenter.defaultCenter().addObserver(self, selector : "dusk:", name:"DuskTheme", object: nil) 以及 dusk(notification: NSNotification) 函数包含我链接的导航栏的新颜色 Storyboard。

这是我的自定义导航 Controller 类:

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)

})
}

}

}

现在由于某种原因,每当滑动 TableView Controller 时,应用程序都会抛出以下异常:

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

这个错误是由手势识别器引起的吗?在对导航 Controller 进行子类化之前,它工作正常。更重要的是,检测主题已更改并更改导航栏颜色的更好方法是什么?

提前致谢!

最佳答案

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)
}
}

关于ios - 为什么我的 NSNotificationCenter 抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30684002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com