UIViewController之间的传值 6种

 

导航控制器下UIViewController之间的传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值 block传值 通知传值 

标签: UIdelegate传值
  5156人阅读  评论(4)  收藏  举报
  分类:
 

1.代理(delegate)传值 ---- 顾名思义就是委托别人办事,就是当一件事情发生后,自己不处理,让别人来处理。

    实质就是:比如右AB两个页面,A想要传值给B ,就只要先在A中得到B的指针,然后将想要传的值赋给B,之后跳转

代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. A.h  
  2. @protocol HMTShowViewControllerDelegate <NSObject>  
  3.   
  4. @optional  
  5. - (void)showViewGiveValue:(NSString *)text;  
  6. @end  
  7.   
  8. @interface HMTShowViewController : UIViewController <UITextFieldDelegate>  
  9.   
  10. @property (nonatomic,copy)NSString * text;  
  11. // 定义一个代理  
  12.   
  13. @property (nonatomic,assign)id<HMTShowViewControllerDelegate> delegate;  
  14.   
  15. @end  
  16.   
  17. A.m  
  18. - (void)popPerView:(UIBarButtonItem *)barButton{  
  19.   
  20.     // 在页面跳转前将参数传出去  
  21.     if ([self.delegate respondsToSelector:@selector(showViewGiveValue:)]) {  
  22.         [self.delegate showViewGiveValue:_showTextField.text];  
  23.     }  
  24.     [self.navigationController popViewControllerAnimated:YES];  
  25.       
  26. }  
  27.   
  28. B.h  
  29. @interface HMTInputViewController : UIViewController <HMTShowViewControllerDelegate>  
  30.   
  31. B.m  
  32. - (void)pushNextViewControl:(UIBarButtonItem *)button{  
  33.   
  34.     HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
  35.     showVC.text = _textField.text;  
  36.     // 将代理对象设置成B  
  37.     showVC.delegate = self;  
  38.     [self.navigationController pushViewController:showVC animated:YES];  
  39.     [showVC release];  
  40.   
  41. }  
  42.   
  43. // B实现协议里面的方法  
  44. - (void)showViewGiveValue:(NSString *)text{  
  45.   
  46.     _inputLabel.text = text;  
  47. }  

2.单例传值 ------- 如果页面之间相隔很多,要进行传值,将值保存到第三方,将第三方设置为单例模式

   代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. static HMTInputViewController * shareRootViewController = nil;  
  2.   
  3. @implementation HMTInputViewController  
  4.   
  5. +(HMTInputViewController *)sharedController{  
  6.     @synchronized(self){  
  7.         if(shareRootViewController == nil){  
  8.             shareRootViewController = [[self alloc] init] ;  
  9.         }  
  10.     }  
  11.     return shareRootViewController;  
  12. }  
  13.   
  14. 要将HMTInputViewController中的一个UITextField编辑的内容传到HMTShowViewController中的UILabel  
  15.  _showLabel.text = [HMTInputViewController sharedController].textField.text;  

3.Target-Action传值

  实质就是:A页面要给B页面传值,A就提供接口出去,抓A到B内部来,A间接调用自己内部方法(相当于,A把自己内部需                     要操作的方法,传到B内来,到B内部进行赋值,这样就不存在访问不到各自的局部实例变量)

      @property (nonatomic,assign)id traget;  @property (nonatomic,assign)SEL action;

       [self.traget performSelector:self.action withObject:nil];(是否需要传参数自己定,最多2个)

代码如下:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. A.h  
  2. @interface HMTShowViewController : UIViewController <UITextFieldDelegate>  
  3.   
  4. @property (nonatomic,assign) id traget;  
  5. @property (nonatomic,assign) SEL action;  
  6.   
  7. @end  
  8.   
  9. A.m  
  10. - (void)popPerView:(UIBarButtonItem *)barButton{  
  11.   
  12.     [self.traget performSelector:self.action withObject:_showTextField.text];  
  13.     [self.navigationController popViewControllerAnimated:YES];  
  14.   
  15. }  
  16.   
  17. B.m  
  18. - (void)pushNextViewControl:(UIBarButtonItem *)button{  
  19.   
  20.     HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
  21.     showVC.traget = self;  
  22.     showVC.action = @selector(changeLabelText:);  
  23.     [self.navigationController pushViewController:showVC animated:YES];  
  24.   
  25. }  
  26.   
  27. - (void)changeLabelText:(NSString *)text{  
  28.       
  29.     _inputLabel.text = text;  
  30.       
  31. }  

4.属性/方法传值

   A - pushViewController - B     在A视图中有B视图的对象 ,可直接操作   

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. - (void)didClickRightButtonItemAction  
  2. {  
  3.     InputViewController * inputVC = [[InputViewController alloc] init];  
  4.   
  5.     [self.navigationController pushViewController:inputVC animated:YES];  
  6.   
  7.     [inputVC release];  
  8. }  
    

    B - popViewControllerAnimated - A    因为A推出B,A只是隐藏了,退到栈底去了,在B返回A,A的viewDidLoad并不会再次调用

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. - (void)didClickLeftButtonItemAction  
  2. {  
  3.     ShowViewController * showView = (ShowViewController *)[self.navigationController.viewControllers objectAtIndex:0];  
  4.       
  5.     [self.navigationController popViewControllerAnimated:YES];  
  6. }  



5.block传值(A-view 传值给 B-view)

@释放    Block_release(_doTransferMsg);

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. A.h  
  2. @property (nonatomic,copyvoid(^doTransferMsg)(NSString * msg);  
  3.   
  4. A.m  
  5. - (void)popPerView:(UIBarButtonItem *)barButton{  
  6.   
  7.     if (_doTransferMsg) {  
  8.         _doTransferMsg(_showTextField.text);  
  9.           
  10.     }  
  11.       
  12.     [self.navigationController popViewControllerAnimated:YES];  
  13. }  
  14.   
  15.   
  16. B.m  
  17. - (void)pushNextViewControl:(UIBarButtonItem *)button{  
  18.   
  19.     HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
  20.       
  21.     // block传值  
  22.     [showVC setDoTransferMsg:^(NSString * msg) {  
  23.         dispatch_async(dispatch_get_main_queue(), ^{  
  24.             _inputLabel.text = msg;        });  
  25.     }];  
  26.       
  27.     [self.navigationController pushViewController:showVC animated:YES];  
  28.     [showVC release];  
  29.   
  30. }  



6.通知传值(同上,也是A传给B)

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. A.m  
  2. - (void)popPerView:(UIBarButtonItem *)barButton{  
  3.   
  4.     [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabelTextNotification" object:_showTextField.text];  
  5.       
  6.     [self.navigationController popViewControllerAnimated:YES];  
  7. }  
  8.   
  9. B.m  
  10. - (void)viewDidLoad  
  11. {  
  12.     [super viewDidLoad];  
  13.       
  14.     // 通知中心,发送一条消息传值--------其中name千万不要写错了,会出现在3个地方  
  15.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showViewGiveValue:) name:@"changeLabelTextNotification" object:nil];  
  16.       
  17. }  
  18.   
  19. - (void)showViewGiveValue:(NSNotification *)notification{  
  20.     // 取出notification的object  
  21.     id text = notification.object;  
  22.     _inputLabel.text = text;  
  23. }  
  24.   
  25. -(void)dealloc{  
  26.     // 消息发送完,要移除掉  
  27.     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeLabelTextNotification" object:nil];  
  28.     [super dealloc];  
  29.       
  30. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值