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
        }
}
 
相关推荐
一块小土坷垃1 小时前
# 《电影猎手》观影伴侣:一款支持iOS/安卓/电视盒子的全平台影视工具“电影猎手”(附自用评价)
android·ios·电视盒子
敲代码的鱼哇3 小时前
发送短信/拨打电话/获取联系人能力 UTS 插件(cz-sms)
android·前端·ios·uni-app·安卓·harmonyos·鸿蒙
报错小能手8 小时前
Swift EventBus讲解
开发语言·ios·swift
四眼蒙面侠8 小时前
Open Agent SDK (Swift):用原生 Swift 构建 AI Agent 应用
swift·agentsdk
报错小能手9 小时前
Swift UI 用 MVVM 架构 Combine EventBus 实现待办清单
开发语言·ui·swift
HiDev_11 小时前
iOS 蓝牙开发进阶:彻底理解 CBManager(状态、权限与正确使用方式)
ios·objective-c·蓝牙·ble
文件夹__iOS1 天前
SwiftUI 核心选型:class + ObservableObject VS struct + @State
ios·swiftui·swift
SameX1 天前
独立开发了一款健康记录 App,聊聊几个让我纠结很久的设计决策
ios
报错小能手1 天前
Swift UI 框架 实战 简易计数器、待办清单 、随机壁纸图库、个人笔记
ui·ios
游戏开发爱好者81 天前
深入理解iOSTime Profiler:提升iOS应用性能的关键工具
android·ios·小程序·https·uni-app·iphone·webview