IOS delegation and Protocol differences and contacts (=)
The last piece roughly describes the differences and connections between the delegates and protocols on iOS, and a simple example, but the example is simple, and today is an example of using a delegate to simulate a button callback.
Put a login button on a custom view, and the LoginView has a delegate object that implements Ilogin, invoking the protocol function that needs to be implemented in the Click event of the login button. Implement the Ilgin protocol in a viewcontroller and implement the login method. Put the custom LoginView in Viewcontroller, and then click the button to call the login method back automatically, and in Viewcontroller you don't see the direct call to the login method, like a system callback.
Code implementation:
ILogin.h
?
| 1 2 3 4 5 6 7 8 9 10 |
#import <Foundation/Foundation.h> @protocol ILogin <NSObject> @required - ( void )login; @optional @end |
Custom View LoginView
?
| 1 2 3 4 5 6 7 |
#import <UIKit/UIKit.h> #import "ILogin.h" @interface LoginView : UIView @property (nonatomic)id<ILogin> delegate; @end |
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#import "LoginView.h"@implementation LoginView- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setFrame:CGRectMake(30, 60, 150, 45)]; [button setTitle:@ "LoginView登陆" forState:UIControlStateNormal]; [button addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } return self; }/* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */- ( void )submitClick{ //这里注意,调用实现协议的类的login方法 [self.delegate login]; }@end |
Viewcontroller
?
| 1 2 3 4 5 6 |
#import <UIKit/UIKit.h> #import "ILogin.h" @interface ViewController : UIViewController<ILogin> @end |
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#import "ViewController.h" #import "LoginView.h" @interface ViewController ()@end@implementation ViewController- ( void )viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. LoginView *loginView=[[LoginView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)]; loginView.delegate=self; [self.view addSubview:loginView]; } - ( void )didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }#pragma mark - ILogin 协议实现 - ( void )login{ NSLog(@ "实现协议函数调用!" ); } @end |
Click button to invoke Result: 2013-08-08 19:49:43.974 delegatedemo[2205:c07] Implement the protocol function call!
What you see now is a system callback, but this is the system callback that takes advantage of the button buttons, normally we typically declare the object of the principal (LoginView) in the object that implements the Protocol (Viewcontroller), Then call the principal's method directly to implement the callback (note: The principal's method contains the protocol method that needs to be implemented). This approach should be a combination of delegates and protocols that are often used in iOS development.
? IOS delegation and Protocol differences and contacts (=)