1. 程式人生 > >iOS-代理托付的使用

iOS-代理托付的使用

cati ima you ces orange pos sed object 協議

#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-代理托付的使用