简体   繁体   English

iOS设置拨动开关第一次起作用,但不再起作用

[英]iOS settings toggle switch works the first time but not again

I am using the iPad settings app to change some button sounds and a background image. 我正在使用iPad设置应用程序来更改一些按钮声音和背景图像。 It all works well and the settings are maintained from one app launch to another in the simulator. 一切运行良好,并且设置从一个应用程序启动到模拟器中的另一个应用程序都得以维护。 Now I have implemented a toggle switch to either set sets of sounds off or on. 现在,我已经实现了一个拨动开关,以关闭或打开声音集。 When the app launches, whatever state the switch is in, it works; 当应用启动时,无论开关处于什么状态,它都可以工作; eg if the "Alert Sounds" switch is OFF the alert sounds are silent and if I change it to ON the sounds will start working. 例如,如果“警报声音”开关为OFF,则警报声音为静音,如果我将其更改为ON,声音将开始工作。 However, if I turn the switch back OFF the sounds still keep working. 但是,如果我将开关重新关闭,声音仍会继续起作用。 However, if the state is ON when the app launches, the sounds work, but will not be silenced when the switch is set to OFF. 但是,如果在应用程序启动时状态为ON,则声音会起作用,但在将开关设置为OFF时不会静音。

Note that this is different than the settings not taking effect until a second round of settings. 请注意,这与直到第二轮设置才生效的设置不同。 That was a previous problem I solved (thanks to stack overflow) by using: 那是我以前通过使用以下方法解决的问题(由于堆栈溢出):

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] synchronize];
}

I have methods named: 我的方法名为:

- (void)defaultsChanged:(NSNotification *)NSUserDefaultsDidChangeNotification

(which is called when the notification is sent) (在发送通知时调用)

and

-(void)setValuesFromPreferences

(which is called in ViewDidLoad) (在ViewDidLoad中称为)

The logic looks like this in both: 两者的逻辑如下:

// Set alert sounds from preferences
NSString *alertSoundPreference = [userDefaults stringForKey:kAlertSound];

BOOL alertSoundEnabled = [userDefaults boolForKey:kAlertSoundEnabled];

if (alertSoundEnabled) 
{
// Create the URLs for the alert audio files
// Store the alert sound URLs as a CFURLRef instances
// Create system sound objects representing the alert sound files
    }

I do not have an else, because I assume that no sound resources will be specified if alertSoundEnabled is NO. 我没有别的,因为我假设如果alertSoundEnabled为NO,则不会指定声音资源。

I have searched for explanations and tutorials that mention this problem but have not found any yet, so I'm asking here. 我已经搜索了提及此问题的解释和教程,但还没有找到,所以我在这里问。 Thanks for any suggestions. 感谢您的任何建议。

viewDidLoad is not necessarily called when the app becomes active again (nor does viewWill/DidAppear , IIRC), as the whole point of iOS 4+ multitasking is to prevent such loading/unloading and recreation of objects on app-switching. 当应用再次激活时,不一定调用viewDidLoadviewWill/DidAppear ,IIRC也未激活),因为iOS 4+多任务的整个目的是为了防止在应用切换时这种对象的加载/卸载和重新生成。

If I had to guess, the sounds are already allocated when the user had the switch ON at original launch/ viewDidLoad ; 如果我不得不猜测的话,当用户在原始的launch / viewDidLoad处打开时,声音已经分配了。 however, if your code does nothing to explicitly disassociate them when it loads back up, they would continue playing, as they are all already set up. 但是,如果您的代码在加载后没有采取任何措施来明确解除它们的关联,则它们将继续播放,因为它们已经全部设置好了。

As such, I'd try adding an else clause that (upon alertSoundEnabled == NO ) destroys your system sound objects. 因此,我尝试添加else子句(在alertSoundEnabled == NO )破坏您的系统声音对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM