简体   繁体   English

NSUserDefaults无法完全在应用程序中进行设置

[英]NSUserDefaults Not Fully Working For Settings In App

I have a settings view in my app that will give the user the option of one of the Tabs showing either a feed of mp3s from a podcast or mov from a podcast. 我的应用程序中有一个设置视图,该视图将为用户提供以下选项卡之一:显示来自播客的mp3或来自播客的mov的选项卡。 I set it up on the first time running the app to display an AlertView asking what they prefer. 我在首次运行该应用程序时进行了设置,以显示一个AlertView询问他们喜欢什么。 I do that with this in the applicationDidFinishLaunching: 我在applicationDidFinishLaunching中这样做:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:@"notFirstRun"]) {
    UIAlertView *firstrun = [[UIAlertView alloc] initWithTitle:@"Sermon Preference" message:@"Do you prefer audio only, or video sermons?  (This setting can be changed at any time in the Settings Page.)" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Audio", @"Video", nil];
    [firstrun show];
    [firstrun release];
    [defaults setBool:YES forKey:@"notFirstRun"];
}

Then I set this in the AppDelegate 然后我在AppDelegate中设置

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0)       {   
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *nope = @"Audio";
        [defaults setObject:nope forKey:@"videosermons"];
        [defaults synchronize];


    }
    if (buttonIndex == 1)        {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *yup = @"Video";
        [defaults setObject:yup forKey:@"videosermons"];
        [defaults synchronize];        }

    }

On the Root View (Which is the audio listing of sermons) of the Navigation Controller for sermons I set this in viewWillAppear: 在布道导航控制器的根视图(布道的音频列表)上,我在viewWillAppear中设置了以下内容:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];


NSString *currently = [defaults objectForKey:@"videosermons"];
if ([currently isEqualToString:@"Video"]) {
    self.videoView = [[[VideoPodcastTableView alloc] initWithNibName:@"VideoPodcastTableView" bundle:[NSBundle mainBundle]] autorelease];


    [self.navigationController pushViewController:_videoView animated:NO];    }
if ([currently isEqualToString:@"Audio"]) {
}

I also set up a Settings Tab with a segmentControl to reflect what has been selected. 我还设置了带有segmentControl的“设置”选项卡,以反映已选择的内容。 This is the issue: 这是问题:

If I click on Video in the firstRun popup, and then go straight to the Sermons tab, it stays on Audio Sermons. 如果在firstRun弹出窗口中单击“视频”,然后直接转到“讲道”选项卡,它将停留在“音频讲道”上。 I can then navigate to the Settings Tab and it will show Video selected. 然后,我可以导航到“设置”选项卡,它将显示“已选择视频”。 Now, without selecting anything, I can go once more to the Sermons tab, and it will now go to the Video Sermons. 现在,无需选择任何内容,我可以再次转到“讲道”选项卡,现在它将进入“视频讲道”。 Why is it that it is not getting the message to change until after I go to the settings? 为什么直到我进入设置后才让消息更改?

The problem is that you're already loading your root view before the UIAlertView clickedButtonAtIndex: selector is called. 问题在于,在调用UIAlertView clickedButtonAtIndex:选择器之前,您已经在加载根视图。 After you launch your UIAlertView in your AppDelegate, your application continues loading your root view in the background. 在AppDelegate中启动UIAlertView之后,您的应用程序将继续在后台加载根视图。

viewWillAppear will have already run by the time a UIAlertView option has been selected. 在选择UIAlertView选项时, viewWillAppear将已经运行。

To solve this, I'd recommend having another root view as your initial view. 为了解决这个问题,我建议您使用另一个根视图作为初始视图。 If you're using storyboards, have one segue going to a view controller that launches your alert view, and another segue going to your tab page. 如果您使用情节提要板,则将一个segue转到启动警报视图的视图控制器,然后将另一个segue转到选项卡页面。 On first launch, segue to the alert view page, and otherwise segue to the tab page. 首次启动时,请选择警报视图页面,否则请选择选项卡页面。 This way your NSUserDefaults @"videosermons" key can be set before the tab view is loaded. 这样,可以在加载选项卡视图之前设置NSUserDefaults @"videosermons"键。

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

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