多控制器之間的跳轉和數據存儲

多控制器之間的跳轉和數據存儲

  • 連線跳轉方式,根據綁定的 ID 進行控制器跳轉數組

[self performSegueWithIdentifier:@"jumpToContact" sender:nil];
  • 而後系統會調用atom

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

向下一個控制器順序傳遞數據,能夠在此方法中編寫spa

  • 代碼方式跳轉code

/** 取出 storyboard 中 ID 爲"edit"的控制器*/UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
XBEditViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"edit"];/** 向控制器傳遞數據 */vc.contact = self.contacts[indexPath.row];
vc.block = ^{
    [self.tableView reloadData];
};/** 壓棧跳轉控制器 */[self.navigationController pushViewController:vc animated:YES];
  • Modalorm

  • 效果:默認是新控制器從屏幕的最底部往上鑽,直到蓋住以前的控制器爲止對象

加載新控制器同步

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion

關閉控制器string

- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;

plist 數據存儲

  • Plist注意:不能存儲自定義對象it

  • Plist:數組和字典io

  • 如何判斷一個對象能不能使用Plist,就看下有沒有writeToFile

    NSArray *arr = @[@"1234",@1];    // 獲取應用的文件夾(應用沙盒)
    //    NSString *homePath = NSHomeDirectory();

    // 獲取temp
    //    NSTemporaryDirectory();

    // 獲取Cache文件路徑
    // NSSearchPathDirectory:搜索的目錄
    // NSSearchPathDomainMask:搜索範圍 NSUserDomainMask:表示在用戶的手機上查找
    // expandTilde 是否展開全路徑,若是沒有展開,應用的沙盒路徑就是~
    // 存儲必定要要展開路徑
    NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];    // 拼接文件名
    NSString *filePath = [cachePaht stringByAppendingPathComponent:@"personArr.plist"];    // File:文件的全路徑
    [arr writeToFile:filePath atomically:YES];    // 文件讀取
    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
  • 完成寫入


寫入效果圖

偏好設置存儲

  • 偏好設置存儲好處:
    1 不須要關心文件名
    2 快速作鍵值對存儲

  • 底層:就是封裝了一個字典

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]
    ;

    [userDefaults setObject:@"sxb" forKey:@"account"];
    [userDefaults setObject:@"123" forKey:@"password"];
    [userDefaults setBool:YES forKey:@"rmbPwd"];    // 在iOS7以前,默認不會立刻把跟硬盤同步
    // 手動同步//    [userDefaults synchronize];
  • 讀取

    NSString *pwd = [[NSUserDefaults standardUserDefaults] objectForKey:@" password"];

自定義對象的歸檔

  • 歸檔能夠存儲本身定義的對象

Person *p = [[Person alloc] init];
    p.age = 18;    // 獲取cache
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];    // 獲取文件的全路徑
    NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];    // 把自定義對象歸檔
    [NSKeyedArchiver archiveRootObject:p toFile:filePath];    // 解檔
    Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
  • 同時 若是一個自定義對象想要歸檔,必須遵照NSCoding協議

    @interface Person : NSObject<NSCoding>
  • 而且實現協議,描述歸檔和解檔的屬性

@implementation Person// 何時調用:自定義對象歸檔的時候// 做用:用來描述當前對象裏面的哪些屬性須要歸檔- (void)encodeWithCoder:(NSCoder *)aCoder
{    // name
    [aCoder encodeObject:_name forKey:@"name"];    // age
    [aCoder encodeInt:_age forKey:@"age"];

}// 何時調用:解檔對象的時候調用// 做用:用來描述當前對象裏面的哪些屬性須要解檔// initWithCoder:就是用來解析文件的。- (id)initWithCoder:(NSCoder *)aDecoder
{    // super:NSObject

    if (self = [super init]) {        // 注意:必定要給成員變量賦值
        // name
       _name = [aDecoder decodeObjectForKey:@"name"];        // age
       _age = [aDecoder decodeIntForKey:@"age"];

    }    return self;

}
相關文章
相關標籤/搜索