I have this simple class
TestViewController.h
#import <UIKit/UIKit.h>
#import "ClassB.h"
@protocol TestViewControllerDelegate <NSObject>
-(void)hello;
@end
@interface TestViewController : UIViewController
@property (nonatomic , weak) id<TestViewControllerDelegate> delegate;
@end
TestViewController.m
#import "TestViewController.h"
@interface TestViewController ()
@end
@implementation TestViewController
@synthesize delegate = _delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
/******* WHAT SHOULD I WRITE HERE?!?? *****/
[self.delegate hello];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
And another class:
ClassB.h
#import <Foundation/Foundation.h>
#import "TestViewController.h"
@interface ClassB : NSObject <TestViewControllerDelegate>
@end
ClassB.m
#import "ClassB.h"
@implementation ClassB
-(void)hello
{
NSLog(@"HELLO!");
}
@end
Now I need to create and instance of ClassB and set this instance as the delegate of TestViewContoller, but I can’t figure out what I need to write!
Can You help me?
What you have written there is correct. The only other thing you need to do is set ClassB as the delegate of TestViewController. One way that’s commonly done is for ClassB to instantiate TestViewController, then set itself as the delegate:
In ClassB: