zoukankan      html  css  js  c++  java
  • Snail—UI学习之自己定义通知NSNotification

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

    在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
    

    效果例如以下


  • 相关阅读:
    RSA加密
    各种正则
    用Fragment制作的Tab页面产生的UI重叠问题
    Android源码下载
    Android Studio使用百度地图问题总结
    Android获取网络类型
    Android Studio类中实现Serializable自动生成serialVersionUID
    【Android开发】如何设计开发一款Android App
    UIViewController生命周期
    微信支付开发经验分享
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6900270.html
Copyright © 2011-2022 走看看