首页 > 代码库 > iOS-代理托付的使用

iOS-代理托付的使用

#import "Rigester_ViewController.h"


#import "Rigester_ViewController.h"


@interface Rigester_ViewController ()


@end


@implementation Rigester_ViewController

*

正向传值能够用属性

反向传值代理Block

 

 代理:又叫托付 自己不能办得事托付给别人去干

 UIAlertView,UITextField 都使用了代理    

 

写代理的步骤:

 

    // 须要代理,托付的人

 

    1.声明代理里面的协议方法(@protocol

    2.声明协议的属性

    3.什么时候须要触发这个代理方法

    4.通过协议的属性调用代理方法(托付)

 

    // 代理者。被托付的人须要做的事

 

    5.导入协议

    6.在初始化有代理方法的对象的地方 挂上代理(代理者响应代理)

    7.写上代理方法 等待被运行

 

 

  

*/


- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = _titleName;

    

    

    UIButton *rigesterButton = [UIButton buttonWithType:UIButtonTypeCustom];

    rigesterButton.frame = CGRectMake(150, 550, 100, 50);

    [self.view addSubview:rigesterButton];

    rigesterButton.backgroundColor = [UIColor orangeColor];

    rigesterButton.showsTouchWhenHighlighted = YES;

    rigesterButton.layer.cornerRadius = 10;

    [rigesterButton setTitle:@"注冊" forState:UIControlStateNormal];

    

    [rigesterButton addTarget:self action:@selector(toRigester) forControlEvents:UIControlEventTouchUpInside ];

    

    

}


//什么时候触发这个代理方法

- (void)toRigester

{

    

    [self.navigationController popViewControllerAnimated:YES];

    

//    4.通过协议的属性调用代理的方法

    [self.delegate toLoginWithName:@"JZQ"];

    

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


@interface Rigester_ViewController ()


@end


@implementation Rigester_ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = _titleName;

    

    

    UIButton *rigesterButton = [UIButton buttonWithType:UIButtonTypeCustom];

    rigesterButton.frame = CGRectMake(150, 550, 100, 50);

    [self.view addSubview:rigesterButton];

    rigesterButton.backgroundColor = [UIColor orangeColor];

    rigesterButton.showsTouchWhenHighlighted = YES;

    rigesterButton.layer.cornerRadius = 10;

    [rigesterButton setTitle:@"注冊" forState:UIControlStateNormal];

    

    [rigesterButton addTarget:self action:@selector(toRigester) forControlEvents:UIControlEventTouchUpInside ];

    

    

}


//什么时候触发这个代理方法

- (void)toRigester

{

    

    [self.navigationController popViewControllerAnimated:YES];

    

//    4.通过协议的属性调用代理的方法

    [self.delegate toLoginWithName:@"JZQ"];

    

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


iOS-代理托付的使用