2013-06-27 23 views
1

我有一個主要的類,我想定義兩個協議(一個由類A使用,另一個由類B使用)(ios 6.1,xcode 4.6.3,ARK模式,故事板項目)。在一個類中的objective-c多協議正確的語法

根據官方語法,我所有的代碼似乎都是正確的。 但是當我嘗試正確使用第二委託,沒有什麼工作,我的第二個代表沒有迴應

**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

你並不需要在類的接口上重新聲明協議方法符合協議'UIViewController '就足夠了,這樣編譯器就會產生混淆,並且很可能只看到最後一個不是協議聲明,因爲是同一個方法標誌兩次 – Manu

+0

那就是答案:) – karim

+0

那麼,如果我明白你說的話,我必須刪除classB頭文件中doOtherStuff方法的聲明,對嗎? 我已經做到了,沒有任何變化,總是同樣的情況。但是,謝謝你的快速答案! –

回答

0

所以,我的錯誤是調用[_myDelegate2 doOtherStuff ..]直接調用只能通過myProtocols函數內部CLASSA。 然後,如果我想用兩個委託我必須初始化這兩個代表在類中調用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); 
} 
相關問題