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

    RootViewController.h

    #import "ModalViewController.h"
    
    @interface RootViewController : UIViewController<ModalViewDelegate>
    

    RootViewController.m

    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController {
    
        ModalViewController *modalCtrl;
        
    }
    
    - (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];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 20)];
        textLabel.tag = 100;
        textLabel.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:textLabel];
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(50, 150, 100, 30);
        [button setTitle:@"打开" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        
        modalCtrl = [[ModalViewController alloc] init];
        //设置代理对象
        modalCtrl.delegate = self;
        
    }
    
    - (void)buttonAction
    {
        [self presentViewController:modalCtrl animated:YES completion:NULL];
        
    }
    
    //实现协议方法
    - (void)responseData:(NSString *)text {
    
        UILabel *label = (UILabel *)[self.view viewWithTag:100];
        
        label.text = text;
        
    }
    

    ModalViewController.h

    //返回文本数据
    - (void)responseData:(NSString *)text;
    
    @end
    
    @interface ModalViewController : UIViewController <UITextFieldDelegate>
    
    @property(nonatomic, assign)id<ModalViewDelegate> delegate;
    ModalViewController.m
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.view.backgroundColor = [UIColor greenColor];
        
        UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(50, 60, 160, 30)];
        textFiled.tag = 100;
        textFiled.delegate = self;
        textFiled.borderStyle = UITextBorderStyleRoundedRect;
        [self.view addSubview:textFiled];
        //显示键盘
        [textFiled becomeFirstResponder];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(50, 150, 100, 30);
        [button setTitle:@"返回" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
    }
    
    //按钮点击事件
    - (void)buttonAction
    {
        [self dismissViewControllerAnimated:YES completion:NULL];
        
        UITextField *field = (UITextField *)[self.view viewWithTag:100];
        NSString *text = field.text;
        
        //推断代理是否实现了协议方法
        if ([self.delegate respondsToSelector:@selector(responseData:)]) {
            //调用协议方法
            [self.delegate responseData:text];
        }
        
    }
    
    //点击return调用的协议方法
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
        //收起键盘
        [textField resignFirstResponder];
        
        return YES;
        
    }
    


  • 相关阅读:
    1.4 build命令
    2.2-2 文章模块开发【添加文章页面脚本编写】
    2.2-1 文章模块开发 【入口脚本及模板的创建】
    2.1 开始一个项目 【功能梳理】
    [微信小程序]不在以下合法域名列表中
    [微信小程序]swiper保持宽高比
    爸爸一路走好
    LVM日记
    欲玩Discuz_X3.2,无奈不支持php7,再装个php5.3,编译到一半报错
    /sbin/ldconfig: /usr/local/lib64/libstdc++.so.6.0.22-gdb.py 不是 ELF 文件
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5262118.html
Copyright © 2011-2022 走看看