Last active
November 14, 2022 02:33
-
-
Save lamprosg/5232113 to your computer and use it in GitHub Desktop.
(iOS) Creating custom delegates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Class will be declared later | |
@class CustomClass; | |
//Define the protocol for the delegate | |
@protocol CustomClassDelegate | |
@required | |
//Required methods go here | |
//If there are optional functions, they go here | |
@optional | |
-(void)sayHello:(CustomClass *)customClass; | |
@end | |
//The NSObject protocol contains a method, respondsToSelector: | |
//that can be used to ensure the delegate object actually implements an optional method before the method is called | |
@interface CustomClass : NSObject | |
//Delegate property | |
@property (nonatomic, weak) IBOutlet id <CustomClassDelegate> delegate; | |
// define public functions | |
-(void)helloDelegate; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "CustomClass.h" | |
@implementation CustomClass | |
@synthesize delegate; | |
-(id)init { | |
self = [super init]; | |
return self; | |
} | |
//Calling this method will cause the sayHello: method to be triggered | |
-(void)helloDelegate { | |
//Verify that the delegate is set and the delegate optional method is implemented (so app won't crash) | |
if (delegate != nil && [delegate respondsToSelector:@selector(sayHello:)]){ | |
[[self delegate] sayHello:self]; | |
} | |
} | |
-(void)dealloc { | |
[super dealloc]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import our custom class | |
#import "CustomClass.h" | |
@interface ViewController : UIViewController <CustomClassDelegate> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation ViewController | |
//Initialization | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
CustomClass *custom = [[CustomClass alloc] init]; | |
//Assign delegate | |
custom.delegate = self; | |
//Calling the helloDelegate will cause the sayHello: to be triggered | |
[custom helloDelegate]; | |
} | |
-(void)dealloc | |
{ | |
[custom release]; | |
[super dealloc]; | |
} | |
//Implementing the custom delegate function | |
-(void)sayHello:(CustomClass *)customClass { | |
NSLog(@"Hiya!"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Click here for set custom delegate and protocol method in swift 4.0