iOS 设置切换开关第一次可以工作,但第二次就不行了

发布于 2024-12-02 18:55:32 字数 1227 浏览 3 评论 0原文

我正在使用 iPad 设置应用程序来更改一些按钮声音和背景图像。一切都运行良好,并且从一个应用程序启动到模拟器中的另一个应用程序启动,设置都得到维护。现在我已经实现了一个切换开关来设置声音组的关闭或打开。当应用程序启动时,无论开关处于何种状态,它都会工作;例如,如果“警报声音”开关关闭,警报声音将保持静音,如果我将其更改为“打开”,声音将开始工作。但是,如果我将开关重新关闭,声音仍然会继续工作。但是,如果应用程序启动时状态为 ON,则声音会起作用,但当开关设置为 OFF 时不会静音。

请注意,这与直到第二轮设置才生效的设置不同。这是我之前解决的问题(感谢堆栈溢出),方法是:

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

我有名为:(

- (void)defaultsChanged:(NSNotification *)NSUserDefaultsDidChangeNotification

在发送通知时调用)

-(void)setValuesFromPreferences

(在 ViewDidLoad 中调用)

的方法,两者的逻辑如下所示:

// 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
    }

我不有一个else,因为我假设如果alertSoundEnabled 为NO,则不会指定任何声音资源。

我已经搜索了提到这个问题的解释和教程,但还没有找到,所以我在这里问。感谢您的任何建议。

I am using the iPad settings app to change some button sounds and a background image. 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; e.g. if the "Alert Sounds" switch is OFF the alert sounds are silent and if I change it to ON the sounds will start working. 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.

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)

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2024-12-09 18:55:32

当应用程序再次激活时,不一定会调用 viewDidLoad(viewWill/DidAppear,IIRC 也不会),因为 iOS 4+ 多任务处理的全部目的是防止此类加载/在应用程序切换时卸载和重新创建对象。

如果我不得不猜测,当用户在原始启动时打开开关时,声音已经被分配/viewDidLoad;但是,如果您的代码在重新加载时没有执行任何操作来明确取消它们的关联,那么它们将继续播放,因为它们都已设置完毕。

因此,我会尝试添加一个 else 子句(在 alertSoundEnabled == NO 时)破坏您的系统声音对象。

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.

If I had to guess, the sounds are already allocated when the user had the switch ON at original 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文