我有一个主类,我想在其中定义两个协议(一个由 A 类使用,另一个由 B 类使用)(ios 6.1、xcode 4.6.3、ARK 模式、情节提要项目)。
根据官方语法,我所有的代码似乎都是正确的。但是当我尝试使用第二个代表时,没有任何工作正常,我的第二个代表没有响应
**HEADER myProtocols.h**
#import ...
@class myProtocols;
@protocol myProtocol1 <NSObject>
// list of methods and properties
doStuff:(float) myValue;
@end
@protocol myProtocol2 <NSObject>
// list of methods and properties
doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
@end
@interface myProtocols:NSObject
{
__unsafe_unretained id <myProtocol1> _myDelegate1;
__unsafe_unretained id <myProtocol2> _myDelegate2;
}
@property (nonatomic, assign) id <myProtocol1> myDelegate1;
@property (nonatomic, assign) id <myProtocol2> myDelegate2;
@end
**MESSAGES myProtocols.m**
#import myProtocols.h
@implementation myProtocols
@synthesize myDelegate1 = _myDelegate1
@synthesize myDelegate2 = _myDelegate2
...
if ([_myDelegate1 respondsToSelector:@selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:@selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:@"YES MAN" andType:@"YES BRO"];
**// THIS DELEGATE DONT WORK, IT'S LIKE IT DOESNT INIT**
...
@end
**HEADER classA.h**
#import "myProtocols.h"
@interface classA: UIViewController <myProtocol1>
@property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doStuff:(float) myValue; according to comments, nothing to do :(
@end
**MESSAGES classA.m**
#import "classA.h"
@interface classA ()
@end
@implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
}
-(void) doStuff:(float) myValue
{
NSLog(@" YES VALUE IS %f",myValue);
}
**HEADER classB.h**
#import "myProtocols.h"
@interface classB: UIViewController <myProtocol2>
@property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType; according to comments, nothing to do :(
@end
**MESSAGES classB.m**
#import "classB.h"
@interface classB ()
@end
@implementation classB
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate2 = self;
}
-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
{
NSLog(@" YES VALUE IS %f and text %@ and type %@",myValue2,myText,myType);
}