码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 代理协议传值

时间:2015-04-03 17:33:55      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

从ViewController跳转到FirstViewController, 在FirstViewController中传一个NSInteger值回来


ViewController.m

#import "ViewController.h"

#import "FirstViewController.h"


@interface ViewController () <PassValueDelegate>

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

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

    self.view.backgroundColor = [UIColor whiteColor];

    _myButton = [UIButton buttonWithType:UIButtonTypeCustom];

    _myButton.frame = CGRectMake(20, 20, 100, 100);

    [_myButton setTitle:@"跳转" forState:UIControlStateNormal];

    _myButton.backgroundColor = [UIColor redColor];

    [_myButton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_myButton];

}

// button点击事件 跳转到FirstViewController

- (void)clicked:(UIButton *)button

{

    FirstViewController *firVC = [[FirstViewController alloc] init];

    firVC.passDelegate = self;

    [self presentViewController:firVC animated:YES completion:nil];

}


- (void)setValue:(NSInteger )value

{

   NSLog(@"value ===== %ld", (long)value);

}


@end







FirstViewController.h

// 签协议  以及方法(名子自己起,类型为要传的值的类型)

@protocol PassValueDelegate <NSObject>

- (void)setValue:(NSInteger)value;

@end


@interface FirstViewController : UIViewController

// 签代理  属性

@property(nonatomicassign) id<PassValueDelegate> passDelegate;

@property(nonatomicassign) NSInteger number;

@end



FirstViewController.m

#import "FirstViewController.h"


@interface FirstViewController ()


@end


@implementation FirstViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor greenColor];

    

    _myButton = [UIButton buttonWithType:UIButtonTypeCustom];

    _myButton.frame = CGRectMake(20, 20, 100, 100);

     [_myButton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_myButton];


    _number = 1000;

}


//代理遵从协议的方法

- (void)clicked:(UIButton *)button

{

    [self.passDelegate setValue:_number];

    [self dismissViewControllerAnimated:YES completion:nil];

}



iOS 代理协议传值

标签:

原文地址:http://blog.csdn.net/liruixuesongxueqian/article/details/44854953

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!