我相信,使用指示性变量是克服这个问题的最优雅的方法。但是这个变量必须保存在委托调用者对象中。
伪类解释
@interface DelegateCaller
{
BOOL hasCalled1stMethod;
}
@property(nonatomic,weak) id delegate;
@end
@implementation DelegateCaller
-(void)in_some_process_1
{
[self.delegate delegateMethod1]; //call
hasCalled1stMethod = YES; //set indicator
}
-(void)in_some_process_2
{
if(hasCalled1stMethod)
{
[self.delegate delegateMethod2]; //call
hasCalled1stMethod = NO; //reset indicator for reuse, if required.
}
}
@end
这样您就不必在委托本身中维护任何变量,因为调用的规则是在调用者对象本身中维护的。
另一种情况:如果从某个object1调用delegateMethod1,从某个其他object2调用delegateMethod2,那么指示性变量方法再次是最优雅的方式(在这个有限的场景中)
伪类解释:
@interface ClassDelegateObject //aka the callee
{
BOOL hasCalledMethod1;
}
@end
@implementation ClassDelegateObject
-(void)delegateMethod1:(NSData*)data
{
self.data = data;
hasCalledMethod1 = YES; //set the indicator.
}
-(void)delegateMethod2
{
//here relying on the self.data!=nil will not be fruitful
//in case the self.data is not nil and hold some previous garbage data then
//this logic will fail.
if(hasCalledMethod1)
{
[someClass sendData:self.data];
hasCalledMethod1 = NO; //reset the variable for reuse if required.
}
}
@end