gpt4 book ai didi

ios - swift : Why localization sometime need to add observer to get it work but sometime don't

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:01:56 32 4
gpt4 key购买 nike

我已经开发了一个需要使用本地化的应用程序,所以我选择了 this nice library为我的申请。但我只是混淆了他们的文档中提到他们需要使用观察者通知来收听语言何时发生变化。但是在我的主 Controller 中,我只是按照他们说的去做,它就像一个魅力一样工作,但是当我将本地化添加到我的侧边栏文本时。我收到这个错误:

原因:'-[NSConcreteNotification 长度]:发送到实例 0x107cbb9f0 的无法识别的选择器'

但是如果我删除侧边栏 Controller 内的观察者。它会工作正常。 所以我的问题是为什么有些 Controller 需要添加一个观察者才能工作,而有些则不需要添加它来工作

这是 MySidebar Controller :

override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc func setText(status: String) {
item = "item".localized(using: "NavigationDrawerViewController")
}

这是选择语言 Controller :

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
Localize.setCurrentLanguage(availableLanguages[indexPath.row])
mytable.reloadData()
}

如果我的英语看起来很困惑,请提前道歉

最佳答案

选择器的签名必须将 Notification 作为参数而不是 String。像这样

@objc func setText(notification: Notification) {

}

来自文档

Selector that specifies the message the receiver sends observer to notify it of the notification posting. The method specified by aSelector must have one and only one argument (an instance of NSNotification).

有关详细信息,请参阅 addObserver(_:selector:name:object:)

关于ios - swift : Why localization sometime need to add observer to get it work but sometime don't,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51977425/

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