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)]; //调用用于传递参数的协议方法 } }