我有这个简单的课程
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
另一堂课:
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
现在我需要创建和实例化ClassB并将此实例设置为TestViewContoller的委托,但我无法弄清楚我需要编写什么! 你能帮帮我吗?
答案 0 :(得分:1)
你所写的内容是正确的。您需要做的唯一事情是将ClassB设置为TestViewController的委托。通常做的一种方法是让ClassB实例化TestViewController,然后将自己设置为委托:
在ClassB中:
TestViewController *test = [[TestViewController alloc] init];
test.delegate = self;