I'm trying to figure out delegates because I really need them for a project I'm working on, but for the life of me, I can't figure them out. No matter how much I tweak the code, nothing works
ViewController.h:
#import <UIKit/UIKit.h>
@class ViewController;
@protocol testDelegate
-(void)sayHi;
@end
@interface ViewController : UIViewController
- (IBAction)button:(id)sender;
@property (weak, nonatomic)id <testDelegate> delegate;
@end
ViewController.m:
#import "ViewController.h"
#import "DelegateController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
DelegateController *dc = [[DelegateController alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button:(id)sender {
[self.delegate sayHi];
}
@end
DelegateController.h:
#import "ViewController.h"
@interface DelegateController : UIViewController <testDelegate>
@end
DelegateController.m:
#import "DelegateController.h"
@interface DelegateController ()
@end
@implementation DelegateController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"init");
ViewController *vc = [[ViewController alloc] init];
[vc setDelegate:self];
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
}
-(void)sayHi
{
NSLog(@"hi");
}
@end
The - (IBAction)button:(id)sender method is connect to a button, but when clicked I get no output. What am I doing wrong?