2015年9月30日 星期三

iOS筆記:NSUserDefaults & UIAlertView in iOS

NSUserDefaults,可以簡單的紀錄一些資訊,說簡單也是能紀錄所有Cocoa上的所有Object(除了自訂的資料型態,要自行處理)。
NSUserDefaults is a way to store information in our Device, that we don’t have to add info. over and over again when we launch app.
NSUserDefaults支持的數據類型有:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL.
Ex. 例如,如果我想要存儲一個 NSMutableArray 對象,我必須先創建一個不可變數組(NSArray)再將它存入NSUserDefaults中去,代碼如下:
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"123",@"234", nil];
    NSArray * array = [NSArray arrayWithArray:mutableArray];

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:array forKey:@"記住存放的一定是不可變的"];
 NSMutableArray *spaceObjectsAsPropertyLists = [[[NSUserDefaults standardUserDefaults] arrayForKey:ADDED_SPACEOBJECTS_KEY] mutableCopy];
    #mutableCopy: in order to transform it from an NSArray to an NSMutableArray.
    if (!spaceObjectsAsPropertyLists) {
        spaceObjectsAsPropertyLists = [[NSMutableArray alloc] init];
    }
    [spaceObjectsAsPropertyLists addObject:[self spaceObjectAsAPropertyList:spaceObject]];
    [[NSUserDefaults standardUserDefaults] setObject:spaceObjectsAsPropertyLists forKey:ADDED_SPACEOBJECTS_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize]; 
    #save the mutableArray that we have just added.
在viewdidload中新增forloop讓原本就新增過的數據可以再打開APP後重現出來.
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *myPlanetsAsPropertyLists = [[NSUserDefaults standardUserDefaults] arrayForKey:ADDED_SPACEOBJECTS_KEY];
    for (NSDictionary *dictionary in myPlanetsAsPropertyLists) {
        CMSpaceObject *spaceObject = [self spaceObjectForDictionary:dictionary];
        [self.addedSpaceObjects addObject:spaceObject];
    }
- (CMSpaceObject *)spaceObjectForDictionary: (NSDictionary *)dictionary
{
    NSData *dataForImage = dictionary[PLANET_IMAGE];
    UIImage *spaceObjectImage = [UIImage imageWithData:dataForImage];
    CMSpaceObject *spaceObject = [[CMSpaceObject alloc] initWithData:dictionary andImage:spaceObjectImage];
    return spaceObject;
}

UIAlertViewController in iOS

在新版本的iOS中UIAlertView已經不支援了. 改為支援UIAlertViewController.
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error" message:@"Username or password combination does not work" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
enter image description here
參考資料:
1. http://fanli7.net/a/bianchengyuyan/C__/20140727/522751.html
2. http://stackoverflow.com/questions/28070015/dismissviewcontrolleranimated-does-not-work-within-a-block
3. http://www.cocoachina.com/ios/20141126/10320.html

沒有留言:

張貼留言