iOS 开发实训第十三周周报

一、学习笔记

  • 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);
      }
      

二、参考链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值