亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

iOSProgrammingNSUserDefaults

2019-11-14 18:57:38
字體:
來源:轉載
供稿:網友

iOS PRogramming NSUserDefaults?

When you start an app for the first time, it uses its factory settings. As you use it, a good app learns your preferences. Where are your preferences stored? Inside each app bundle there is a plist that holds the user's preferences. As a developer, you will access this plist using the NSUserDefaults class. The preferences plist for your app can also be edited by the Settings app. To allow this, you create a settings bundle inside your app.

當你第一次開始你的app時,你使用它的工廠設置。當你使用它時,一個好的app 會學習你的preferences.你的preferences 存在哪了呢?在每個app bundle中都有一個plist 保持了user的preference.作為一個developer,你可以通過使用NSUserDefaults類。preferences plist 你的app能夠通過setting app 編輯。為了允許這個,你需要創建一個setting bundle 在你的app內。

1 NSUserDefaults

The set of defaults for a user is a collection of key-value pairs. The key is the name of the default, and the value is some data that represents what the user prefers for that key.You ask the shared user defaults object for the value of that key – not unlike getting an object from a dictionary:

對一個用戶的defaults 的設置是一個key-value pairs 容器。key 是default的名字,而value是代表餓了用戶喜歡什么的數據。你詢問shared user defaults 對象為了那個key 的value,很像從一個字典中獲取對象。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *greeting = [defaults objectForKey:@"FavoriteGreeting"];

If the user expresses a preference, you can set the value for that key:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:@"Hello" forKey:@"FavoriteGreeting"];

This value will automatically be stored to the app's preferences plist. Thus, the value must be a plist type: NSArray, NSDictionary, NSString, NSData, NSDate, or NSNumber. If you want to store a non-plist type to the user defaults, you will need to convert it to a plist. Often this is accomplished by archiving the object (or objects) into an NSData, which is a plist.

這些值自動的存入app的preference plist.因此這個value必須是plist type:NSArray,NSDictionary,NSString,NSData,NSDate,or NSNumber.如果你是存的一個non-plist,你需要把它轉換為plist。通常這會存檔一個對象為NSData。

What if you ask for the value of a preference that has not been set by the user? NSUserDefaults will return the factory settings, the "default default," if you will. These are not stored on the file system, so you need to tell the shared instance of NSUserDefaults what the factory settings are every time your app launches. And you need to do it early in the launch process – before any of your classes try to read the defaults. Typically you will override +initialize on your app delegate:

如果你詢問一個preference的值還沒有被設置?NSUserDefaults將會返回factory settings ,the "default default",如果你樂意。這些沒有存儲到文件系統中,所以你需要告訴NSUserDefaults 的shared instance? 在你的app 每次啟動時要設置什么factory settings .你需要做這些在啟動程序的早期:在你的類視圖讀取defaults之前。一般你回重+initialize 在你的app delegate上。

+ (void)initialize

{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSDictionary *factorySettings = @{@"FavoriteGreeting": @"Hey!",

@"HoursBetweenMothershipConnection : @2};

[defaults registerDefaults:factorySettings];

}

The class method initialize is called automatically by the Objective-C runtime before the first

instance of that class is created.

這個類方法initialize 在那個類被創建的Objective -C 運行時間自動的被調用。

1.1 Register the factory settings

注冊factory Settings

At launch time, the first thing that will happen is the registering of the factory settings. It is considered good style to declare your preference keys as global constants.

啟動時,將發生的第一件事就是factory settings 的注冊。聲明你的preference keys 為一個global? constants 是一種好的方式。Open BNRAppDelegate.h and declare two constant global variables:

extern NSString * const BNRNextItemValuePrefsKey;

extern NSString * const BNRNextItemNamePrefsKey;

In BNRAppDelegate.m, define those global variables and use them to register the factory defaults in

+initialize:

NSString * const BNRNextItemValuePrefsKey = @"NextItemValue";

NSString * const BNRNextItemNamePrefsKey = @"NextItemName";

?

@implementation BNRAppDelegate

+ (void)initialize

{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSDictionary *factorySettings = @{BNRNextItemValuePrefsKey: @75,

BNRNextItemNamePrefsKey: @"Coffee Cup"};

[defaults registerDefaults:factorySettings];

}

?

1.2 Read a preference ? ? ?讀取一個preference

When you create a new item in BNRItemStore.m, use the default values. Be sure to import BNRAppDelegate.h at the top of BNRItemStore.m so that the compiler knows about BNRNextItemValuePrefsKey.

?

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

item.valueInDollars = [defaults integerForKey:BNRNextItemValuePrefsKey];

item.itemName = [defaults objectForKey:BNRNextItemNamePrefsKey];

// Just for fun, list out all the defaults
NSLog(@"defaults = %@", [defaults dictionaryRepresentation]);

Notice the method integerForKey:. It is there as a convenience. It is equivalent to:

item.valueInDollars = [[defaults objectForKey:BNRNextItemValuePrefsKey] intValue];

There are also convenience methods for setting and getting float, double, BOOL, and NSURL values.

1.3 Change a preference

You could create a view controller for editing these preferences. Or, you could create a settings bundle for setting these preferences. Or, you can just try to guess the user's preferences from their actions. For example, if the user sets the value of an item to $100, that may be a good indication that the next item might also be $100. For this exercise, you will do that.

你可以創建一個view controller 來編輯這些preferences.或者,你能創建settings? bundle 來設置這些preferences.或者你僅僅視圖猜測用戶的preferences 從他們的actions。例如,一個用戶設置一個item的value是100,那么這是一個很好的indication ,next item 可能也是100。

Open BNRDetailViewController.m and edit the viewWillDisappear: method.

?

int newValue = [self.valueField.text intValue];

// Is it changed?

if (newValue != item.valueInDollars) {

// Put it in the item item.valueInDollars = newValue;

// Store it as the default value for the next item

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults setInteger:newValue ?? forKey:BNRNextItemValuePrefsKey];

item.valueInDollars = [self.valueField.text intValue];

}

?

Import BNRAppDelegate.h so that the compiler knows about the BNRNextItemValuePrefsKey constant.

Sometimes you will give the user a button that says "Restore factory default", which will remove some defaults from the app's preferences plist.

To remove key-value pairs from your app's preferences plist, NSUserDefaults has a removeObjectForKey: method.

有時候你提供給用戶一個button 說重新設置factory default,這將移除所有的default在你的app的preferences plist.移除所有的key-value pairs 從你的app's preferences plist,NSUserDefaults 有一個removeObjectForKey方法。

?

2 Settings Bundle?

Now you are going to create a settings bundle so that the NextItemName preference can be changed from "Coffee Cup" to whatever string the user desires.

現在你將創建一個settings bundle 這樣NextItemName preference 能從"Cofffee Cup"到任何用戶需要的數據。

These days many designers consider settings bundles to be distasteful and most apps do not include a settings bundle. That said, many apps do have settings bundles, so it is a good idea to know how to make them.

這些天,許多designers 認為setting bundles 為不好的,許多apps 不包括setting bundles .也就是許多apps 有settings bundles ,所以知道怎么用她是有用的。

The bundle is just a directory that holds a plist that describes what controls should appear in this view and what default each control is manipulating.?

bundle 僅僅是擁有了一個plist表示了什么控制應該出現在這個view和每個控制默認的操作是什么的一個目錄而已。

You will pull the user visible strings (like the label "Default Item Name" in
Figure 26.1) into a strings file that is localized for the user. Those strings files will also be in the settings bundle.

你將把user visible strings 放進一個Strings? file用來用戶本地化。這些Strings files 也將在settings bundle.

To create a settings bundle inside your app, open Xcode's File menu and choose New → File.... Under the iOS Resources pane, choose Settings Bundle

?

Accept the default name. Notice that a directory called Settings.bundle has been created in your project directory. It has a Root.plist file and an en.lprog subdirectory.

1.1 Editing the Root.plist

The Root.plist describes what controls will appear in your app's settings pane. It contains an array of dictionaries; each dictionary represents one view (typically a control) that will appear on the pane. Every dictionary must have Type key. Here are the acceptable values for Type:

Root.plist表述了什么控制將顯示到你的app's setting pane。它包含了一列dictionaries;每個dictionary 代表了一個view將顯示在pane上的。每個dictionary 必須有一個Type key。這里是一些能接受的值:

PSTextFieldSpecifier

a labeled text field 標簽文字

PSToggleSwitchSpecifier

a labeled toggle switch 開關標簽

PSSliderSpecifier

a slider (not labeled) 滑動?

PSRadioGroupSpecifier

a list of radio buttons; only one can be selected 一列radio buttons,只有一個可選

PSMultiValueSpecifier

a table view of possibilities; only one can be selected 一個table view 的可能

PSTitleValueSpecifier

a title for formatting

PSGroupSpecifier

a group for formatting

PSChildPaneSpecifier

lets you move some preferences onto a child pane

讓你移動一些preferences 到child pane.

Notice that you have been laying out a user interface using a plist file. When you create a settings bundle, you are not writing any executable code, and you are not creating any view controllers or other objects that you control. The Settings application will read your application's Root.plist and will construct its own view controllers based on the contents of the plist file.

注意到你已經laying out 一個user interface用一個plist file.當你創建一個setting bundle時,你沒有寫任何可執行的代碼,你也沒有創建然任何view controller 或者是你控制的其他對象。setting application 會讀取你應用的root.plist并構建自己的view controller 基于plist file的內容。

2.2 Localized Root.strings

Inside your settings bundle is an en.lproj which will hold your English strings. You can delete all the key-value pairs and give the title for your text field:

在你的setting bundle是一個en.lproj,它將保持你的English strings.你可以刪除所有的key-value pairs 并給定你的text field 的title。

"NextItemName" = "Default Item Name";

One final point: when your defaults are changed (by your own app or the Settings app) an NSUserDefaultsDidChangeNotification will get posted to your application. If you want to respond to changes in the Settings app immediately, register as an observer of this notification.

最后一點是:當你的defaults 發生改變時,NSUserDefaultsDidChangeNotification 將推送給application。如果你想及時的響應setting app 立刻的,注冊成為這個notification 的一個觀察者。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美成在线观看| 亚洲精品少妇网址| 亚洲日本aⅴ片在线观看香蕉| 欧美—级高清免费播放| 久久成人亚洲精品| 国产日韩欧美日韩大片| 国产成人精品999| 久久九九热免费视频| 视频在线一区二区| 亚洲国产精品人人爽夜夜爽| 欧美国产日产韩国视频| 亚洲二区在线播放视频| 国产精品综合网站| 日韩网站免费观看高清| 亚洲综合av影视| 亚洲免费视频网站| 亚洲全黄一级网站| 国产亚洲精品成人av久久ww| 欧美亚洲日本黄色| 亚洲成人a**站| 97国产成人精品视频| 成人精品一区二区三区电影免费| 国产精品视频网址| 久久久噜久噜久久综合| 91精品国产沙发| 日韩精品在线视频美女| 国产精品网站入口| 欧美精品激情在线| 国产精品日韩电影| 欧美日韩国产中文精品字幕自在自线| 欧美在线视频一区| 日韩在线不卡视频| 欧美精品激情在线| 欧美激情va永久在线播放| 国产精品久久久久久中文字| www国产精品视频| 久久精品99无色码中文字幕| 久久免费视频网站| 日韩精品久久久久久久玫瑰园| 超薄丝袜一区二区| 中文亚洲视频在线| 亚洲欧美日韩一区二区三区在线| 日本国产一区二区三区| 国产日韩精品电影| 精品女厕一区二区三区| 欧美高清一级大片| 免费97视频在线精品国自产拍| 成人免费看黄网站| 国产成人一区三区| 日韩综合视频在线观看| 51精品国产黑色丝袜高跟鞋| 国产成人一区二| 亚洲天堂男人天堂女人天堂| 国产精品久久久久久久久久久久| 国产有码在线一区二区视频| 久久精品国产视频| 亚洲人成毛片在线播放| 在线观看欧美日韩国产| 日本精品va在线观看| 97久久精品人搡人人玩| 国产自摸综合网| 久久亚洲欧美日韩精品专区| 日本sm极度另类视频| 日韩精品在线影院| 欧美色播在线播放| 亚洲国产精品成人va在线观看| 成人精品一区二区三区| 热久久这里只有精品| 亚洲欧美综合另类中字| 91精品国产91久久久久久不卡| 亚洲欧美日韩中文视频| 久久久在线观看| 第一福利永久视频精品| 亚洲综合社区网| 久久久av电影| 久久综合伊人77777尤物| 欧美亚州一区二区三区| 国产精品日韩电影| 国产精品v片在线观看不卡| www国产精品视频| 国产在线观看一区二区三区| 国产精品6699| 亚洲欧美日韩国产中文专区| 久久精品国产亚洲一区二区| 一本色道久久综合狠狠躁篇怎么玩| 日韩激情视频在线| 亚洲人午夜精品| 亚洲欧美资源在线| 亚洲欧美在线磁力| 91精品久久久久久久久不口人| 色噜噜狠狠狠综合曰曰曰88av| 国产极品精品在线观看| 欧美日韩亚洲激情| 久久国产色av| 久久中文字幕一区| 精品久久久久久久大神国产| 国产精品久久不能| 亚洲国产精品成人av| 亚洲男人第一网站| 色yeye香蕉凹凸一区二区av| 欧美人在线观看| 亚洲伊人久久综合| 2018中文字幕一区二区三区| 国产97在线亚洲| 久久免费精品日本久久中文字幕| 欧美孕妇孕交黑巨大网站| 国产精品日韩在线播放| 久久精品免费电影| 欧美黄色片免费观看| 国产精品91久久久| 777午夜精品福利在线观看| 国产精品主播视频| 亚洲精品日韩激情在线电影| 国产在线精品一区免费香蕉| 久久精品视频亚洲| 一本色道久久88综合亚洲精品ⅰ| 琪琪第一精品导航| 久久久精品2019中文字幕神马| 亚洲欧美成人在线| 国产一区私人高清影院| 国产欧美婷婷中文| 久久久久久欧美| 亚洲精品国产精品自产a区红杏吧| 国产一区二区三区在线看| 日韩av在线一区| 亚洲第一区中文字幕| 久久久久久久网站| 欧美性猛交xxxx免费看久久久| 日韩精品在线观| 伊人青青综合网站| 久久成年人视频| 97香蕉超级碰碰久久免费的优势| 国产日韩欧美自拍| 国产精品久久久久久久久久久久久久| 亚洲精品国产精品国自产观看浪潮| 国产精品成人aaaaa网站| 久久久精品网站| 成人做爰www免费看视频网站| 欧美一级片免费在线| 日韩精品一区二区三区第95| 国产精品亚洲精品| 日韩视频欧美视频| 欧美日韩美女在线观看| 国产日韩欧美中文在线播放| 日韩亚洲第一页| 久久精品国产精品亚洲| 久久成人人人人精品欧| 一区二区欧美在线| 国产精品网站视频| 青青草国产精品一区二区| 久久视频免费观看| xxav国产精品美女主播| 久久久av免费| 亚洲精品成人久久电影| 麻豆国产va免费精品高清在线| 欧美日韩成人免费| 亚洲欧洲一区二区三区久久| 欧美激情视频给我| 日日骚久久av| 亚洲欧美日韩区| 成人观看高清在线观看免费| 国产精品观看在线亚洲人成网| 欧美日韩国产精品专区| 国产精品成人播放|