zoukankan      html  css  js  c++  java
  • 代理和 block 传值的使用

    //
    //  ZYViewController.h
    //  BlockTest
    //
    //  Created by yejiong on 14/11/2.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    
    
    //1.声明一个 delegate 协议。
    @protocol ZYViewControllerDelegate <NSObject>
    
    //声明一个方法用来传值。
    //返回值表明传值的结果,如果不关心那么就写 void。
    //方法中的参数就是我们需要传的值,如果要传多个值,后面继续追加参数。
    - (void)selectedColor:(UIColor* )color;
    
    @end
    
    @interface ZYViewController : UIViewController
    
    //2.声明一个 delegate 属性。
    @property (nonatomic, assign) id<ZYViewControllerDelegate> delegate;
    
    @end
    //
    //  ZYViewController.m
    //  BlockTest
    //
    //  Created by wanglixing on 14/11/2.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import "ZYViewController.h"
    
    @interface ZYViewController ()
    
    @property (nonatomic, retain) NSArray* colors;
    
    @end
    
    @implementation ZYViewController
    
    - (void)dealloc {
        [_colors release];
        
        [super dealloc];
    }
    
    - (void)viewDidLoad {
        self.view.backgroundColor = [UIColor whiteColor];
        
        //数组包含了所有选项的标题。
        UISegmentedControl* sc = [[UISegmentedControl alloc] initWithItems:@[@"红色", @"绿色", @"蓝色"]];
        
        sc.frame = CGRectMake(20, 100, 200, 44);
        
        //使用 UIControlEventValueChanged
        [sc addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
        
        [self.view addSubview:sc];
        
        [sc release];
        
        self.colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
    }
    
    - (void)valueChanged:(UISegmentedControl* )sender {
        NSLog(@"%@", @(sender.selectedSegmentIndex));
        
        //3.使用 delegate 对象调用协议方法进行传值。
        if ([_delegate respondsToSelector:@selector(selectedColor:)]) {
            //调用方法前需要判断 delegate 是否实现方法。
            [_delegate selectedColor:_colors[sender.selectedSegmentIndex]];
        }
    }
    
    
    @end

    //实现代理

    //
    //  ViewController.m
    //  BlockTest
    //
    //  Created by yejiong on 14/11/2.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "ZYViewController.h"
    
    @interface ViewController () <ZYViewControllerDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        self.view.backgroundColor = [UIColor whiteColor];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
      ZYViewController* vc = [[ZYViewController alloc] init];
       
      5.设置 delegate 属性。
        vc.delegate = self;
        
       [self.navigationController pushViewController:vc animated:YES];
        
        [vc release];
        
        
    }
    
    #pragma mark - ZYViewControllerDelegate
    
    //4.实现协议方法,获取传过来的值。
    - (void)selectedColor:(UIColor *)color {
        self.view.backgroundColor = color;
    }
    
    
    @end

    使用 Block传值

    //
    //  ZYBlockViewController.h
    //  BlockTest
    //
    //  Created by yejiong on 14/11/2.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    //使用 block 传值分为 4 步。
    
    //1.声明一个 block 类型,返回值表明传值的结果,参数表明传值的数据。
    typedef void(^block)(UIColor* color);
    
    @interface ZYBlockViewController : UIViewController
    
    //2.声明一个 block 属性。
    @property (nonatomic, copy) block block;
    
    
    
    @property (nonatomic, retain) UIColor* selectedColor;
    
    @end
    //
    //  ZYBlockViewController.m
    //  BlockTest
    //
    //  Created by yejiong on 14/11/2.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import "ZYBlockViewController.h"
    
    @interface ZYBlockViewController ()
    
    @property (nonatomic, retain) NSArray* colors;
    
    @end
    
    @implementation ZYBlockViewController
    
    - (void)dealloc {
        [_colors release];
        
        [_block release];
        
        [_selectedColor release];
        
        [super dealloc];
    }
    
    - (void)viewDidLoad {
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        //数组包含了所有选项的标题。
        UISegmentedControl* sc = [[UISegmentedControl alloc] initWithItems:@[@"红色", @"绿色", @"蓝色"]];
        
        sc.frame = CGRectMake(20, 100, 200, 44);
        
        //使用 UIControlEventValueChanged
        [sc addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
        
        [self.view addSubview:sc];
        
        [sc release];
        
        self.colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
        
        NSInteger index = [_colors indexOfObject:_selectedColor];
        
        
        if (index != NSNotFound) {
            sc.selectedSegmentIndex = index;
        }
    }
    
    - (void)valueChanged:(UISegmentedControl* )sender {
        NSLog(@"%@", @(sender.selectedSegmentIndex));
        
        //3.调用 block 进行传值。
        if (_block) {
            //调用 block 前需要判断 block 是否实现。
            _block(_colors[sender.selectedSegmentIndex]);
        }
    }
    
    @end
    //
    //  ViewController.m
    //  BlockTest
    //
    //  Created by wanglixing on 14/11/2.
    //  Copyright © 2014年 zzz. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "ZYBlockViewController.h"
    
    @interface ViewController () <ZYViewControllerDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        self.view.backgroundColor = [UIColor whiteColor];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        
        
        
        ZYBlockViewController* vc = [[ZYBlockViewController alloc] init];
        
        //4.实现 block 并获取传过来的值。
        vc.block = ^(UIColor* color){
            //使用 color。
            self.view.backgroundColor = color;
            
            //写在 push 前,push 后效果都一样,因为是同一个 UINavigationController
    //        [self.navigationController popViewControllerAnimated:YES];
        };
        
        vc.selectedColor = self.view.backgroundColor;
        
        [self.navigationController pushViewController:vc animated:YES];
        
        [vc release];
    }
    
    
    @end
  • 相关阅读:
    ASP.NET简单登录注册实例
    ViewState 视图状态对象实例
    重定向redirect与跳转forward区别
    request请求对象实例
    Page_Load 事件
    JQuery增删改查
    winfrom增删改查
    SQLHelper类
    html弹出div弹窗
    react diff算法
  • 原文地址:https://www.cnblogs.com/ningmengcao-ios/p/5771264.html
Copyright © 2011-2022 走看看