代表团问题

时间:2011-02-25 03:57:16

标签: iphone objective-c delegates

代码结构

@protocal A_Delegate
{
     -(void)doIt:(BOOL)isDone;
}

Super Class A // has properties of set delegate
-(void) setDelegate:(id<A_Delegate>)_delegate
{
       /*self.delegate = _delegate*/ error, compiler stops right there and doesn't assigns the value from '_delegate'

self.delegate = _delegate

       //should be
       delegate = _delegate;
}

Sub Class B : A // want to call and define the delegation for the super class of B which is A
-(void) acquireDelegation:(id<A_Delegate>)_delegate
{
       [[super delegate] setDelegate];
}

现在,C类想要使用B类并想知道它的状态,

Class C : NSObject <A_Delegation>
-(void) doSomething
{
     B *b = [[B alloc]init];
     [b aquireDelegation:self];
}
-(void)doIt:(BOOL)isDone
{
      if(isDone)
      // Do Something
}

有没有人知道我做错了什么以及为什么超级不能授权? 有可能解决吗?

解决。

2 个答案:

答案 0 :(得分:2)

这将导致无限循环(直到堆栈溢出):

-(void) setDelegate:(id<A_Delegate>)_delegate
{
       self.delegate = _delegate; // error, compiler stops right there and doesn't assigns the value from '_delegate'
}

因为self.delegate = _delegate;正在调用setDelegate:。你必须分配给ivar本身。

我认为这不是编译器。这是运行中的一切......

答案 1 :(得分:0)

有几个问题。首先,该协议定义不是一个。语法都错了。其次,什么是“超级A”才是评论?

Anyhoo,主要问题在于setDelegate:方法。如果已定义委托属性,则行:

self.delegate = _delegate;

等于行:

[self setDelegate:_delegate];

因此,在-setDelegate方法中,您调用-setDelegate。