Best practices for handling user preferences in an iPhone MVC app?

后端 未结 1 694
余生分开走
余生分开走 2020-12-17 05:11

I\'m developing my first iPhone app, and while implementing user preferences, I have a few questions on how to best implement this.

Assume the basic MVC pattern: Mai

相关标签:
1条回答
  • 2020-12-17 05:49

    Have a look at NSUserDefaults. It is a dictionary style collection class designed for this particular purpose.

    You store and retrieve defaults like so:

    // Fetch a default
    BOOL deleteBackup = [[NSUserDefaults standardUserDefaults] boolForKey:@"DeleteBackup"];
    
    // Save a default
    [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"DeleteBackup"];
    

    NSUserDefaults will automatically take care of reading from and serializing to disk.

    Concerning your other question, it depends on the overall architecture of your app. Sending out notifications when the OptionsViewController is finished sounds like a reasonable approach to me. However, you could also just define a method on your MainViewController or app delegate (ie. "-reloadUserDefaults") that you can invoke from your OptionsViewController when it's finished.

    0 讨论(0)
提交回复
热议问题