一、学习笔记
-
iOS
页面间传值:-
属性传值:从
A
页面跳转到B
页面,那么在B
页面的控制器里定义对应的属性,在跳转时给这些属性赋值即可,主要用于正向传值// SecondVC.h @interface SecondViewController : UIViewController @property (nonatomic, copy) NSString *str; @end // FirstVC.m - (void)btnClick:(UIButton *)btn { SecondViewController *secondVC = [[SecondViewController alloc] init]; // 定义回调的block secondVC.str = @"属性传值"; [self.navigationController pushViewController:secondVC animated:YES]; }
-
block
传值:从A
页面跳转到B
页面,再从B
页面返回A
页面,可以在B
页面中声明一个block
属性,然后在A
页面的控制器中定义这个block
,在B
页面返回A
页面时调用这个block
,主要用于反向传值// SecondVC.h typedef void (^TestBlock)(NSString *str); @interface SecondViewController : UIViewController // 定义block属性 @property (nonatomic, copy) TestBlock testBlock; @end // SecondVC.m - (void)btnClick:(UIButton *)btn { if (self.testBlock) { self.testBlock(@"block传值"); } [self.navigationController popViewControllerAnimated:YES]; } // FirstVC.m - (void)btnClick:(UIButton *)btn { SecondViewController *secondVC = [[SecondViewController alloc] init]; // 定义回调的block secondVC.testBlock = ^(NSString *str) { NSLog(@"%@", str); }; [self.navigationController pushViewController:secondVC animated:YES]; }
-
delegate
传值:从A
页面跳转到B
页面,再从B
页面返回A
页面,可以在B
页面委托A
页面,即A
页面作为B
页面的代理,主要用于反向传值// SecondVC.h // 声明代理 @protocol DeliverDelegate <NSObject> // 声明可选的方法 @optional - (void)setStr:(NSString *)str; @end @interface SecondViewController : UIViewController @property (nonatomic, weak) id<DeliverDelegate> deliverDelegate; @end // SecondVC.m - (void)btnClick:(UIButton *)btn { if ([self.delegate respondsToSelector:@selector(setStr:)]) { [self.delegate setStr:@"代理传值"]; } [self.navigationController popViewControllerAnimated:YES]; } // FirstVC.m - (void)btnClick:(UIButton *)btn { SecondViewController *secondVC = [[SecondViewController alloc] init]; secondVC.delegate = self; [self.navigationController pushViewController:nextVC animated:YES]; } #pragma mark - DeliverDelegate - (void)setStr:(NSString *)str { NSLog(@"%@", str); }
-
Notification
传值:从A
页面跳转到B
页面,再从B
页面返回A
页面,可以在B
页面发通知,然后再A
页面收通知,主要用于反向传值// SecondVC.m - (void)btnClick:(UIButton *)btn { //发出通知 [[NSNotificationCenter defaultCenter] postNotificationName:@"btnClickNoti" object:self userInfo:@{@"str": @"通知传值"}]; [self.navigationController popViewControllerAnimated:NO]; } // FirstVC.m - (void)viewDidLoad { [super viewDidLoad]; // ... // 注册通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(btnClickNoti:) name:@"btnClickNoti" object:nil]; } // 收到通知的时候调用这个方法接受到通知消息 - (void)btnClickNoti:(NSNotification *)noti { NSDictionary *dict = noti.userInfo; NSLog(@"%@", dict[@"str"]); } // 控制器销毁的时候注销通知监听 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
-
KVO
传值:从A
页面跳转到B
页面,再从B
页面返回A
页面,可以在A
控制器观察B
控制器的属性,在B
页面返回A
页面时修改被观察属性,主要用于反向传值// SecondVC.m @property (nonatomic, copy) NSString *str; - (void)btnClick:(UIButton *)btn { self.str = @"KVO传值"; [self.navigationController popViewControllerAnimated:YES]; } // FirstVC.m @property (nonatomic, strong) SecondViewController *secondVC; - (void)btnClick:(UIButton *)btn { secondVC = [[SecondViewController alloc] init]; // FirstVC观察SecondVC的str属性 [secondVC addObserver:self forKeyPath:@"str" options:NSKeyValueObservingOptionNew context:nil]; [self.navigationController pushViewController:secondVC animated:YES]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { // 此处监听key对应值的变化情况 if ([keyPath isEqualToString:@"str"]) { NSLog(@"%@", self.secondVC.str); } } - (void)dealloc { // 注销观察者,否则会crash [self.second removeObserver:self forKeyPath:@"str"]; }
-
单例传值:这种方法中单例类就相当于一个第三方容器,在
B
页面将数据放到容器里,然后在A
页面读取,可用于正向传值和反向传值// SecondVC.m - (void)btnClick:(UIButton *)btn { // 给单例类属性赋值 ATLoginStatus *status = [ATLoginStatus sharedLoginStatus]; status.isLogin = YES; [self.navigationController popViewControllerAnimated:YES]; } // FirstVC.m - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // 从单例类取值 ATLoginStatus *status = [ATLoginStatus sharedLoginStatus]; NSLog(@"%d", status.isLogin); }
-
UserDefaults
传值:将数据存到本地,然后在各个页面都可以随时获取// SecondVC.m - (void)btnClick:(UIButton *)btn { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:@"UserDefaults传值" forKey:@"str"]; [defaults synchronize]; [self.navigationController popViewControllerAnimated:YES]; } // FirstVC.m - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *str = [defaults objectForKey:@"str"]; NSLog(@"%@", str); }
-
二、参考链接
- ios页面间传值