我試圖學習目標C的協議。我寫了兩個文件,第一個是FirstViewController.h,其中有一個協議「打印」。我使用委託方法「print」在successViewController中聲明FirstViewController類。 問題是控制檯輸出爲「C」的原因。爲什麼我不能得到「B」輸出?爲什麼協議方法沒有執行?一個Objective-C協議的問題
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface successViewController : UIViewController <FirstViewControllerDelegate> {
}
@end
#import "successViewController.h"
#import "FirstViewController.h"
@implementation successViewController
- (void)viewDidLoad {
FirstViewController *firstViewController= [[FirstViewController alloc] init];
firstViewController.delegate=self;
NSLog(@"C");
[super viewDidLoad];
}
-(void) print{
NSLog(@"B");
}
@end
#import <Foundation/Foundation.h>
@class FirstViewController;
@protocol FirstViewControllerDelegate <NSObject>
- (void) print;
@end
@interface FirstViewController : NSObject {
id <FirstViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id <FirstViewControllerDelegate> delegate;
@end
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize delegate;
@end