3、iOS传值的方法-Notifaction正向传值

用Notifaction正向传值,将A界面的值传到B界面。在A类中发送一个通知,在B类中注册一个通知。


1、A类中的代码如下:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@property (retain,nonatomic) UITextField *textField;


@end

@implementation FirstViewController
@synthesize textField;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"Notifaction正向传值";
    
    
    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:textField];

    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 35)];
    btn.backgroundColor = [UIColor grayColor];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

- (void)viewDidDisappear:(BOOL)animated {
    
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"flash" object:self userInfo:@{@"name":textField.text}];
}

- (void)btnClick:(id)sender {
    
    SecondViewController *second = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:second animated:YES];
    
}

@end

2、B类中的代码如下:

#import "SecondViewController.h"

@interface SecondViewController ()


@property (retain, nonatomic) UILabel *label;

@end

@implementation SecondViewController
@synthesize label;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
      
    label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 30)];
    label.text = @"点击按钮";
    [self.view addSubview:label];
    

    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, self.view.frame.size.width-20, 35)];
    btn.backgroundColor = [UIColor grayColor];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(flashString:) name:@"flash" object:nil];
    

}

- (void)flashString:(NSNotification *)notification {
    
    NSDictionary *dict = [notification userInfo];
    label.text = [dict objectForKey:@"name"];
}

- (void)dealloc {
    //注销通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)btnClick:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];

}

@end

3、效果截图如下:

     


4 、Demo下载地址 : http://download.csdn.net/detail/u010545480/8909563

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值