swift - 如何将布尔值从应用程序委托传输到视图控制器?
问题描述
我目前正在尝试将变量从应用程序委托传输到 ViewController。我找到的大多数指南都在 Objective-c 中,我没有将代码转换为 swift 的技能。有人可以帮我找到一种方法来做到这一点。
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
var isIpad: Bool!
if UIDevice.current.userInterfaceIdiom == .pad {
isIpad = true
print("iPad")
}else{
print("not iPad")
isIpad = false
}
}
解决方案
虽然我建议看一下@rmaddy 的第一条评论,但您可以在模块中的任何位置声明一个存储属性(不必在应用程序委托中声明):
let isIpad = UIDevice.current.userInterfaceIdiom == .pad
这意味着不需要将值从应用程序委托传递给视图控制器。因此在视图控制器中——例如——:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if isIpad {
// ...
}
}
}
推荐阅读
- linux-containers - 如何在 Linux 容器中使用 devicemapper
- java - 请提供理解递归的指导
- javascript - 上传mp3文件并在node.js中播放上传的文件
- java - 如何构建迭代器以获取列表中的所有可能组合
- python-3.x - Python中的Caesar Cipher - 如何替换字符
- reactjs - 如何更新组件之间的数据?
- swift - 在 viewWillAppear 中使用 UserDefaults
- java - 如何为 MVVM 模式链接 Retrofit 和 Repository/ViewModel?
- javascript - jQuery UI 可拖动不能在 RTL 页面中拖动 X 轴
- python - 自定义 Python Dominate 标记元素