代理和 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傳值atom

//
//  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
相關文章
相關標籤/搜索