调用协议方法是否通过程序流控制?

Mat*_*ick 1 iphone delegates objective-c custom-protocol ios

我知道这很可能是一个蹩脚的问题,但是我连续三次全力以赴,而且我非常模糊.我是Objective C和Cocoa Touch的新手.

我创建了一个提供委托方法的类.我将使用简化的示例代码,因为细节并不重要.头文件如下所示:

#import <Foundation/Foundation.h>

@protocol UsernameCheckerDelegate <NSObject>
@required
- (void)didTheRequestedThing:(BOOL)wasSuccessful;
@end

@interface TheDelegateClass : NSObject {
    id <TheDelegateClassDelegate> tdcDelegate;
}

@property (assign) id <TheDelegateClassDelegate> tdcDelegate;

- (void)methodThatDoesSomething:(int)theValue;

@end
Run Code Online (Sandbox Code Playgroud)

源文件如下所示:

#import "TheDelegateClass.h"

@implementation TheDelegateClass

@synthesize tdcDelegate;

- (void)methodThatDoesSomething:(int)theValue {
    if (theValue > 10) {
        [[self tdcDelegate] didTheRequestedThing:NO];
        // POINT A
    }

    // POINT B
    int newValue = theValue * 10;
    NSString *subject = [NSString stringWithFormat:@"Hey Bob, %i", newValue];
    // Some more stuff here, send an email or something, whatever

    [[self tdcDelegate] didTheRequestedThing:YES];
    // POINT C
}

@end
Run Code Online (Sandbox Code Playgroud)

我的问题是:如果theValue其实大于10及以上的A点运行线路,并程序流控制传递出这种方法(和回didTheRequestedThing在调用该对象的委托方法)或不流继续通过PO B到POINT C?

我希望前者是因为我可以简化我的代码,目前是一个令人不快的混乱深深嵌套的ifs和elses.

Lil*_*ard 5

当-didTheRequestedThing:方法返回时,控制流返回到POINT A并继续到POINT B和POINT C.委托方法调用与任何其他方法调用完全相同.如果您想在委托调用后避免执行方法的其余部分,只需调用return// POINT A注释所在的位置即可.