iOS 让界面元素的文字随着语言的更改而变化——本地化文字跟随

在我的 App 内置的设置中,修改了语言,这时需要让当前界面的文本跟着改变语言。

解决方法是:添加一个观察者,观察 localize 本地语言的通知,然后一有变化就调用自定义的方法执行操作。(而设置中其实是改变了 localize 的语言)

swift 复制代码
override func viewDidLoad() {
        tableForChoose = tableChoose
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
}
@objc func setText() {
        btn_complete?.setTitle("done".localized(), for: .normal)
        tableForChoose?.reloadData()
        tableForChoose?.selectRow(at: defaultChoose ?? IndexPath(row: 0, section: 0), animated: true, scrollPosition: .middle)
        if let cell = tableForChoose?.cellForRow(at: defaultChoose ?? IndexPath(row: 0, section: 0)) {
            cell.accessoryType = .checkmark
        }
}
 
相关推荐
编程之路从0到13 小时前
ReactNative新架构之iOS端TurboModule源码剖析
react native·ios·源码阅读
SameX8 小时前
春节期间独立开发者从 0 到 1:呼吸训练 iOS App 的工程化落地
ios
东坡肘子17 小时前
春晚、机器人、AI 与 LLM -- 肘子的 Swift 周报 #124
人工智能·swiftui·swift
kevinli4 天前
available没你想象中的可靠
ios·编译原理
BatmanWayne4 天前
swift-微调补充
人工智能·swift
CodeJourney_J4 天前
如何通过SMB协议实现手机共享电脑文件
windows·ios·smb
ipad协议开发5 天前
企业微信iPad协议接口深度解析:技术原理、应用场景与开发实战
ios·企业微信·ipad
千里马-horse5 天前
Building a Simple Engine -- Mobile Development -- Platform considerations
android·ios·rendering·vulkan