NSUserDefauts - 只有新的NSStrings没有出现在应用

时间:2016-06-09 21:59:54

标签: ios objective-c nsuserdefaults

在解释这个问题之前,我想提一下我已经尝试过这些问题的解决方案:

  1. Xcode not building app with changes incorporated
  2. Why can't I delete files from my XCode project?
  3. XCode simulator is not displaying latest changes
  4. 我已经存档我的应​​用程序,它现在已在App Store中提交,但现在我无法进行所需的更改以提供声音更新。该应用程序提供不同主题的统计数据,以及MLA / APA引用和该来源的Web视图。代码以这种方式构建:

    TopicViewController.m为每个统计信息,引用和源URL保存NSString变量。与特定主题UIButtons相关的IBActions,例如Cyber​​security,设置每个NSString变量的内容。最后,我保存NSUserDefaults中所有统计数据,引用和URL的数据并同步它们。 这些信息是我添加新内容的位置

    示例代码(字符串的实际数量非常大):

    @implementation StatsViewController
    
    NSString *cybersecStatistic1;
    NSString *cybersecStatistic2;
    NSString *cybersecStatistic3;
    NSString *cybersecStatistic1CitationMLA;
    NSString *cybersecStatistic1CitationAPA;
    NSString *cybersecStatistic2CitationMLA;
    NSString *cybersecStatistic2CitationAPA;
    NSString *cybersecStatistic3CitationMLA;
    NSString *cybersecStatistic3CitationAPA;
    NSString *cybersecStatistic1Source;
    NSString *cybersecStatistic2Source;
    NSString *cybersecStatistic3Source;
    
    -(IBAction)cybersecurityGo:(id)sender {
        cybersecStatistic1 = @"\"In 2015, nearly 20% of global companies implemented a cybersecurity budget between $1 million and $4.9 million.\" (PricewaterhouseCoopers survey)";
        cybersecStatistic2 = @"The average annual number of cybersecurity incidents is 80-90 million. There was a 38% increase in such incidents from 2014-2015.";
        cybersecStatistic3 = @"USA: Over 36% of people who share passwords have shared the password associated with their banking account.";
    
        cybersecStatistic1CitationAPA = @"Penguin Strategies. (2016, January 17). Top Cybersecurity Statistics for 2016 - Cyber Security Marketing Blog. Retrieved April 16, 2016, from http://www.marketingcyber.com/2016-cybersecurity-statistics/";
        cybersecStatistic2CitationAPA = @"Smith, C. (2016, January 20). 34 Amazing Cybersecurity Statistics. Retrieved April 16, 2016, from http://expandedramblings.com/index.php/cybersecurity-statistics/";
        cybersecStatistic3CitationAPA = @"Norton by Symantec. (1995-2016). Norton Cybersecurity Insights Report. Retrieved April 16, 2016, from https://us.norton.com/cyber-security-insights";
    
        cybersecStatistic1CitationMLA = @"\"Top Cybersecurity Statistics for 2016 - Cyber Security Marketing Blog.\"Cyber Security Marketing Blog. Penguin Strategies, 17 Jan. 2016. Web. 16 Apr. 2016.";
        cybersecStatistic2CitationMLA = @"Smith, Craig. \"34 Amazing Cybersecurity Statistics.\" DMR. DMR, 20 Jan. 2016. Web. 16 Apr. 2016.";
        cybersecStatistic3CitationMLA = @"\"Norton Cybersecurity Insights Report.\" 2016. Norton by Symantec, 1995-2016. Web. 16 Apr. 2016.";
    
        cybersecStatistic1Source = @"http://www.marketingcyber.com/2016-cybersecurity-statistics/";
        cybersecStatistic2Source = @"http://expandedramblings.com/index.php/cybersecurity-statistics/";
        cybersecStatistic3Source = @"https://us.norton.com/cyber-security-insights";
    
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic1 forKey:@"statistic1"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic2 forKey:@"statistic2"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic3 forKey:@"statistic3"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic1CitationMLA forKey:@"statistic1CitationMLA"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic1CitationAPA forKey:@"statistic1CitationAPA"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic2CitationMLA forKey:@"statistic2CitationMLA"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic2CitationAPA forKey:@"statistic2CitationAPA"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic3CitationMLA forKey:@"statistic3CitationMLA"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic3CitationAPA forKey:@"statistic3CitationAPA"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic1Source forKey:@"statistic1Source"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic2Source forKey:@"statistic2Source"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSUserDefaults standardUserDefaults] setObject: cybersecStatistic3Source forKey:@"statistic3Source"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    

    点击主题按钮可转到下一个屏幕StatsViewController。它的实现文件接收NSUserDefaults并显示相应的统计信息。与每个统计UIButton相关的IBActions为下一个屏幕设置“路线”以显示正确的引用。 这是因为有些事情发生错误,因为我所查看的最新状态显示的引用(从未添加新的陈述引文) - >我之前通过将我的ViewDidLoad更改为CitationViewController中的ViewWillLayoutSubviews来修复此问题,我将在下面解释

    @implementation StatsViewController
    
    NSString *whichStat;
    
    NSString *strStat1;
    NSString *strStat2;
    NSString *strStat3;
    
    -(IBAction)stat1Go:(id)sender {
        whichStat = @"stat1Route";
        [[NSUserDefaults standardUserDefaults] setObject:whichStat forKey:@"activeStat"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    -(IBAction)stat2Go:(id)sender {
        whichStat = @"stat2Route";
        [[NSUserDefaults standardUserDefaults] setObject:whichStat forKey:@"activeStat"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    -(IBAction)stat3Go:(id)sender {
        whichStat = @"stat3Route";
        [[NSUserDefaults standardUserDefaults] setObject:whichStat forKey:@"activeStat"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    -(void)viewWillAppear:(BOOL)animated {
    
        whichStat = nil;
        strStat1 = nil;
        strStat2 = nil;
        strStat3 = nil;
    
        strStat1 = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic1"];
        strStat2 = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic2"];
        strStat3 = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic3"];
    
        [_stat1 setTitle:strStat1 forState:UIControlStateNormal];
        [_stat2 setTitle:strStat2 forState:UIControlStateNormal];
        [_stat3 setTitle:strStat3 forState:UIControlStateNormal];
    
        if ([strStat1  isEqual: @"more stats to come"]) {
            _stat1.enabled = false;
        }
        else {
            _stat1.enabled = true;
        }
        if ([strStat2  isEqual: @"more stats to come"]) {
            _stat2.enabled = false;
        }
        else {
            _stat2.enabled = true;
        }
        if ([strStat3  isEqual: @"more stats to come"]) {
            _stat3.enabled = false;
        }
        else {
            _stat3.enabled = true;
        }
    

    点击统计按钮会转到CitationViewController,它会显示APA和MLA引文。 新添加字符串的引用(自应用程序商店提交)未正确加载 ViewWillLayoutSubviews

    @implementation CiteViewController
    
    NSString *citeStatPath;
    
    NSString *citeUsingMLA;
    NSString *citeUsingAPA;
    
    NSString *sourceWebSite;
    
    -(void)viewWillLayoutSubviews {
    
    citeStatPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"activeStat"];
    
        if ([citeStatPath  isEqualToString: @"stat1Route"]) {
            citeUsingMLA = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic1CitationMLA"];
            citeUsingAPA = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic1CitationAPA"];
    
            [_citeMLA setTitle:citeUsingMLA forState:UIControlStateNormal];
            [_citeAPA setTitle:citeUsingAPA forState:UIControlStateNormal];
        }
        else if ([citeStatPath isEqualToString:@"stat2Route"]) {
            citeUsingMLA = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic2CitationMLA"];
            citeUsingAPA = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic2CitationAPA"];
    
            [_citeMLA setTitle:citeUsingMLA forState:UIControlStateNormal];
            [_citeAPA setTitle:citeUsingAPA forState:UIControlStateNormal];
        }
        else {
            citeUsingMLA = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic3CitationMLA"];
            citeUsingAPA = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic3CitationAPA"];
    
            [_citeMLA setTitle:citeUsingMLA forState:UIControlStateNormal];
            [_citeAPA setTitle:citeUsingAPA forState:UIControlStateNormal];
        }
    }
    

    点击引文按钮会转到UIWebView,它从NSUserDefault获取URL并建立并保存在TopicViewController.m中。 正确的网站也没有出现

    @implementation SourceViewController
    
    NSString *srcStatPath;
    
    NSString *webSite;
    
    -(void)viewWillAppear:(BOOL)animated {
    
        srcStatPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"activeStat"];
    
        if ([srcStatPath  isEqualToString: @"stat1Route"]) {
            webSite = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic1Source"];
        }
        else if ([srcStatPath isEqualToString:@"stat2Route"]) {
            webSite = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic2Source"];
        }
        else {
            webSite = [[NSUserDefaults standardUserDefaults] stringForKey:@"statistic3Source"];
        }
    
    NSURL *url = [NSURL URLWithString:webSite];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_source loadRequest:request];
    }
    

    在iPhone模拟器和我自己的iOS设备(iPhone 6-版本9.3.2)上会出现此问题。

    提前感谢您的帮助。我很卡住了!

0 个答案:

没有答案