ViewControlller之间传值

1.ViewController之间正向传值   (ViewController1传给ViewController2)


利用属性的方法:传值2.png       传值1.png



ViewController2中定义一个属性,等ViewController2初始化的时候直接为其属性赋值


   SecondViewController *second = [[SecondViewController alloc] init];

   second.count = i;

   second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    

    [self presentModalViewController:second animated:YES];

    [second release];

2.逆向传值     (ViewController2传给ViewController1)


利用代理的方法:


因为此时要改变viewcontroller1中label的值,所以ViewController1是委托者,委托者需要实现协议中的方法


首先定义一个协议

//MyProtocal.h

#import <Foundation/Foundation.h>

@protocol MyProtocal <NSObject>

- (void)setLabel:(NSString *)str;

@end


ViewController2中定义一个指针指向委托者,其实该指针也就是ViewController1的对象,通过对象调用setLabel:(NSString *)str方法,从而实现ViewController2向ViewController1传值.


3.利用广播的方法


首先发送一个广播

UIColor *color = [UIColor greenColor];

[[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_COLOR" object:color];


接收广播

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(change:) name:@"CHNAGE_COLOR" object:nil];


//实现方法

- (void) change:(NSNotification *)sender

{

UIColor *color = [sender object];

  self.view.backgroundColor = color;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值