应用程序在前景和背景中 [英] App in foreground and background

查看:105
本文介绍了应用程序在前景和背景中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图控制器中有一个按钮ON / OFF,当用户打开它时会播放一些音乐。现在,如果用户单击iPhone主页按钮并再次重新启动我的应用程序,该按钮将显示为开启,但没有播放音乐。因此,用户必须再次点击ON-OFF-ON才能再次开始播放音乐。

I have a button ON/OFF in my viewcontroller that plays some music when it is turned on by the user. Now if the user clicks the iPhone home button and re-launches my app again, the button is shown as "ON" but there is no music playing. So the user has to hit ON-OFF-ON again for music to start playing again.

任何人都知道如何调用我的ON / OFF视图控制器按钮以便我当应用程序进入后台时将其设置为OFF并将其设置为ON并在这些应用程序代表中进入forground时播放音乐?

Anyone know how can I call my ON/OFF view controller button so that I can set it to OFF when app enters background and set it to ON and play the music when it enters forground in these app delegates?

我知道我需要写入一个plist文件按钮& applicationDidEnterBackground 上的音乐状态。我不知道如何从appdelegate获取这些动作,因为它们是在我的viewcontroller中定义的。

I know I need to write to a plist file the button & music state on applicationDidEnterBackground. I don't know how can I get to those action from appdelegate since they are defined in my viewcontroller.

类似,当应用程序进入前台时我会读取已保存的plist文件然后再次设置音乐和按钮的状态。同样,我不知道如何从委托调用我的控制器方法。

Similary, when the app enters the foreground I will read the saved plist file and then set the state of music and button again. Again, I don’t know how to call my controller methods from delegate.

- (void)applicationDidEnterBackground:(UIApplication *)application
{

     NSLog(@"Inside applicationDidEnterBackground");
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

    NSLog(@"Inside applicationWillEnterForeground");

}


推荐答案

所有你需要做的是订阅视图控制器中的UIApplicationDidEnterBackgroundNotification通知

All you need to do is subscribe to the UIApplicationDidEnterBackgroundNotification notification in your view controller

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground:) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

Theres也是DidEnterForeground的通知。

Theres also a notification for DidEnterForeground too.

这篇关于应用程序在前景和背景中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆