0

私はこの単純なクラスを持っています

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 のデリゲートとして設定する必要がありますが、何を書く必要があるのか​​わかりません! 手伝って頂けますか?

4

1 に答える 1

1

あなたがそこに書いていることは正しいです。他に行う必要があるのは、ClassB を TestViewController のデリゲートとして設定することだけです。一般的に行われる 1 つの方法は、ClassB が TestViewController をインスタンス化し、それ自体をデリゲートとして設定することです。

クラス B では:

TestViewController *test = [[TestViewController alloc] init];
test.delegate = self;
于 2012-10-13T21:28:15.940 に答える