objective-c多协议在一个类正确的语法中

时间:2013-06-27 09:20:38

标签: ios objective-c syntax delegates protocols

我有一个主类我要定义两个协议(1个用于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);
}

1 个答案:

答案 0 :(得分:0)

所以,我的错误是直接在myProtocols中只有classA调用的函数内调用[_myDelegate2 doOtherStuff ..]。 然后,如果我想使用两个委托调用myProtocols中的函数,我必须在类中初始化这两个委托(A或B无关紧要)我用来调用这个函数:

**MESSAGES myProtocols.m**
#import myProtocols.h
@implementation myProtocols
@synthesize myDelegate1 = _myDelegate1
@synthesize myDelegate2 = _myDelegate2

-(void) pleaseDoIt 
{
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 NOW WORK VERY WELL**
}
@end

**HEADER classA.h**
#import "myProtocols.h"
#import "classB.h"
@interface classA: UIViewController <myProtocol1>
@property(strong, nonatomic) myProtocols *myProtoVC;
@property(strong, nonatomic) classB *classBVC;

//-(void) doStuff:(float) myValue;
@end

**MESSAGES classA.m**
#import "classA.h"
@interface classA ()
@end
@implementation classA
- (void)viewDidLoad
{
    [super viewDidLoad];
    _myProtoVC = [[myProtocols alloc] init];
    _classBVC = [[myProtocols alloc] init];
    _myProtoVC.myDelegate1 = self;
    _myProtoVC.myDelegate2 = _classBVC   // THIS IS THE POINT!!!

    [_myProtoVC pleaseDoIt];
}
-(void) doStuff:(float) myValue
{
  NSLog(@" YES VALUE IS %f",myValue);
}