1

我知道这很可能是一个蹩脚的问题,但我已经连续三个通宵了,我很模糊。我是 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

源文件如下所示:

#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

这是我的问题:如果theValue实际上大于 10 并且 POINT A 上方的行运行,程序流控制是从该方法传递出去(并返回到调用 this 的对象中的didTheRequestedThing委托方法)还是继续流B点到C点?

我希望是前者,因为我可以简化我的代码,目前是深度嵌套的 if 和 else 令人不快的混乱。

4

1 回答 1

5

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

于 2010-10-28T04:56:12.880 回答