zoukankan      html  css  js  c++  java
  • 通知---视图间数据的传递:标签显示输入的内容【多个视图中】

    RootViewController.m

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            //注冊通知
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(recieveData:)
                                                         name:BackNotification object:nil];
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor greenColor];
        
        //创建显示文字的label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
        label.tag = 102;
        label.backgroundColor = [UIColor grayColor];
        [self.view addSubview:label];
        [label release];
        
        //加入按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(20, 20, 90, 60);
        [button setTitle:@"打开模态" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        
    }
    
    - (void)recieveData:(NSNotification *)notification {
    
        
         UILabel *label = (UILabel *)[self.view viewWithTag:102];
        
        NSString *text = [notification.userInfo objectForKey:@"text"];
        
        label.text = text;
        
    }
    
    - (void)buttonAction {
    
        ModalViewController *modalCtrl = [[[ModalViewController alloc] init] autorelease];
        
        modalCtrl.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        
        [self presentViewController:modalCtrl animated:YES completion:NULL];
        
    }
    

    ModalViewController.m

    #define  BackNotification     @"BackNotification"
    @interface ModalViewController ()
    
    @end
    
    @implementation ModalViewController
    
    - (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 redColor];
        
        //加入按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(20, 20, 90, 60);
        [button setTitle:@"关闭模态" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        //创建输入框
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 40)];
        textField.tag = 101;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:textField];
        [textField release];
        
        
    }
    
    - (void)buttonAction {
        
        UITextField *field = (UITextField *)[self.view viewWithTag:101];
    
        NSString *text = field.text;
        
        NSDictionary *dic = @{@"text":text};
        
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:BackNotification
                                                            object:self userInfo:dic];
        
        
        
        
        [self dismissViewControllerAnimated:YES completion:NULL];
        
    }
    


  • 相关阅读:
    viewer.js 显示图片名称和照片属性
    js中判断数组中是否包含某元素的方法(转载)
    js脚本如何更新, js后加?v=版本号的原因(转载)
    iview Carousel 走马灯或轮播图 点击事件失效
    sqlserver 查询表中所有字段的最大长度(转载)
    Ueditor文字和echarts图片 生成 word 前端解决方案
    Spark Streaming Backpressure分析
    Spark任务调度流程及调度策略分析
    Spark资源调度及任务调度
    spark streaming流式计算---监听器
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6772396.html
Copyright © 2011-2022 走看看