NSUserDefaults提供了一系列方法,能夠存儲一些基本數據類型或對象,具體用法有如下幾個方法:ide
+(NSUserDefaults * _Nonnull) standardUserDefaults
-(void) setObject:(nullable id) forKey:(nonnull NSString *)
-(id _Nullable) objectForKey:(nonnull NSString *)
+(NSData * _Nonnull)archivedDataWithRootObject:(nonnull id)
+(id _Nullable)unarchiveObjectWithData:(nonnull NSData *)
這個方法是NSKeyedUnarchiver的靜態方法,經過這個方法能夠將NSData還原爲原來的數據,這個方法是可失敗的,返回值多是nil。函數
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. }
[segue destinationViewController]
來得到跳轉後的視圖控制器,得到以後,能夠調用這個視圖控制器的方法進行參數傳遞。這時候推薦將該方法定義爲協議。例若有以下的用法:@protocol DataTransferDelegate <NSObject> //該協議規定了視圖之間參數傳遞的方式 @optional -(void)transferBoolValue:(BOOL)state From:(id)sender; -(void)transferString:(NSString *)str From:(id)sender; @end
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier isEqualToString:@"ToLogInSegue"]) { //先對identifier進行判斷 LogInTestViewController *viewController=[segue destinationViewController]; //得到跳轉後的視圖控制器 [viewController transferBoolValue:([_touchidswitch isOn]) From:(self)]; //調用用於傳遞參數的協議方法 } }