zoukankan      html  css  js  c++  java
  • IOS传值之代理传值(一)

    1、使用代理delegate的方法

    2、使用通知Notification的方法

    3、KVO等方法

    4、block传值

    ~~~~~~~~~~~~~~~~

    1、使用代理delegate的方法

    #import "ViewController.h"

    #import "SubViewController.h"

    @interface ViewController ()<subViewDelegate>

    @property(nonatomic,strong)UILabel *label;

    @end

    @implementation ViewController

    -(UILabel *)label{

        if (_label==nil) {

            _label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];

            _label.backgroundColor=[UIColor grayColor];

            //[self.view addSubview:_label];

        }

        return _label;

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        [self.view addSubview:self.label];

        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];

        [button setBackgroundColor:[UIColor grayColor]];

        [button setTitle:@"next" forState:UIControlStateNormal];

        [button addTarget:self action:@selector(nextVC) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

    }

    -(void)nextVC{

        NSLog(@"hhhhhhhhh");

        SubViewController *subVC=[[SubViewController alloc]init];

        subVC.delegate=self;

        //[self.navigationController pushViewController:subVC animated:YES];

        [self presentViewController:subVC animated:YES completion:^{

            NSLog(@"cccccc");

        }];

    }

    -(void)showStringFromSubView:(NSString *)textFieldtext{

        self.label.text=textFieldtext;

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

    ~~~~~~~~~~~~~~~~~~~~~~~

    #import <UIKit/UIKit.h>

    @protocol subViewDelegate <NSObject>

    -(void)showStringFromSubView:(NSString *)textFieldtext;

    @end

    @interface SubViewController : UIViewController

    @property(nonatomic,weak)id<subViewDelegate> delegate;

    @end

    #import "SubViewController.h"

    @interface SubViewController ()

    @property(nonatomic,strong)UITextField *textField;

    @end

    @implementation SubViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        self.view.backgroundColor=[UIColor whiteColor];

        [self.view addSubview:self.textField];

        //backBtn

        UIButton *back= [UIButton buttonWithType:UIButtonTypeCustom];

        back.frame=CGRectMake(100, 200, 50, 50);

        [back setBackgroundColor:[UIColor grayColor]];

        [back setTitle:@"back" forState:UIControlStateNormal];

        [back addTarget:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:back];

    }

    -(void)backToPre{

        [self.delegate showStringFromSubView:self.textField.text];

        [self dismissViewControllerAnimated:YES completion:^{

            NSLog(@"backToPre");

        }];

    }

    -(UITextField *)textField{

        if (_textField==nil) {

            _textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];

            _textField.backgroundColor=[UIColor grayColor];

        }

        return _textField;

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~实现效果:

  • 相关阅读:
    关于移动端点击后出现闪或者黑色背景
    :after伪类+content内容生成经典应用举例
    移动端(html5)微信公众号下用keyup实时监控input值的变化无效
    jquery-uploadify 上传
    SpringMvc 文件上传
    总结
    poi excel导入
    sencha 安装、学习
    sencha怎么在control层调用按钮
    sencha做个简单的登录界面
  • 原文地址:https://www.cnblogs.com/Jordandan/p/4950703.html
Copyright © 2011-2022 走看看