ame*_*ian 8 iphone uiviewcontroller ios swift
我有一个简单的视图应用程序,只有一个主视图.
当应用程序进入后台状态时,我正在尝试在视图内的标签中显示更新的时间值.
场景是:
1 - 单视图应用程序项目
2 - 只有一个带有一个Label的View(ViewController)来显示日期
3 - 在AppDelegate中:applicationWillEnterForeground获取当前时间
func applicationWillEnterForegound(application: UIAPplication){
var date:String = GetDate()
--> update View Label with date here
}
Run Code Online (Sandbox Code Playgroud)
4 - 在ViewController上显示当前时间
我正在尝试代理,但问题是视图是应用程序和方法中的最后一个可见元素,因为viewDidLoad,viewWillAppear被调用一次.
小智 18
正如其他人所说,您可以使用通知框架在视图控制器中执行此操作,这样您的appDelegate就不需要引用您的视图控制器.在控制器中添加如下所示的行:
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(appWillEnterForeground),
name: NSNotification.Name.UIApplicationWillEnterForeground,
object: nil
Run Code Online (Sandbox Code Playgroud)
在Swift 3中,语法略有改变:
NotificationCenter.default.addObserver(self,
selector: #selector(appWillEnterForeground),
name: NSNotification.Name.UIApplicationWillEnterForeground,
object: nil)
Run Code Online (Sandbox Code Playgroud)
然后在选择器中定义您命名的函数:
NotificationCenter.default.addObserver(self,
selector: #selector(appWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4665 次 |
最近记录: |