码迷,mamicode.com
首页 > 其他好文 > 详细

Snail—UI学习之自定义通知NSNotification

时间:2015-07-23 23:54:00      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

背景是:一个界面跳转到第二个界面 然后 第一个界面发了一个通知  然后第二个界面收到这个通知后 把里面的数据取出来

在RootViewController.m中写入下面代码

#import "WJJRootViewController.h"
#import "WJJFirstViewController.h"

@interface WJJRootViewController (){
    UITextField * _textField;
}

@end

@implementation WJJRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
	// Do any additional setup after loading the view.
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 40, 240, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.placeholder = @"请输入:";
    _textField.clearButtonMode = UITextFieldViewModeAlways;
    _textField.keyboardType = UIKeyboardTypeDefault;
    _textField.returnKeyType = UIReturnKeyGo;
    
    //设置textField的代理 要让viewController为他收起键盘
    _textField.delegate = self;
    
    [self.view addSubview:_textField];
    [self createButton];
}

- (void)createButton{
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, 80 , 50, 50);
    button.backgroundColor = [UIColor blackColor];
    [button setTitle:@"点我" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)nextPage{
    //新建一个界面
    WJJFirstViewController * firstViewController = [[WJJFirstViewController alloc] init];
    //设置反转风格
    firstViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    //从第一个界面转到第二个界面 有动画
    [self presentViewController:firstViewController animated:YES completion:nil];
    
    //发一个通知 如果第二个界面接收这个通知的话,就会得到通知里面的数据
    //写在跳转界面之后 这样跳转后第二个界面才可以接收
    [[NSNotificationCenter defaultCenter] postNotificationName:@"1523" object:_textField.text];
}

//那么在第二个界面中就要接收这个通知的话 代码如下

#import "WJJFirstViewController.h"

@interface WJJFirstViewController (){
    UILabel * _label;
}

@end

@implementation WJJFirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    _label = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 200, 50)];
    _label.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_label];
    //get:方法是接收到通知后 做得操作  name:类似频道 在这个频道上就能接收到这个通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(get:) name:@"1523" object:nil];
}

- (void)get:(NSNotification *)noti{
    
    //得到通知里面的对象
    id obj = noti.object;
    NSString * str = (NSString *)obj;
    _label.text = str;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

效果如下

技术分享技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Snail—UI学习之自定义通知NSNotification

标签:

原文地址:http://blog.csdn.net/qq1791422018/article/details/47029411

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!