Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 通过委托调用时无法识别协议方法_Objective C_Ios_Delegates_Protocols - Fatal编程技术网

Objective c 通过委托调用时无法识别协议方法

Objective c 通过委托调用时无法识别协议方法,objective-c,ios,delegates,protocols,Objective C,Ios,Delegates,Protocols,我的问题是,当我通过委托调用协议方法dataLoading时,它只是不识别它——给出了一个预期的标识符错误 以下是协议/接口文件: #import <Foundation/Foundation.h> @class LoaderView; @protocol DataLoaderProtocol <NSObject> @required - (void) dataLoading; - (void) doneLoading; @end @interface Data

我的问题是,当我通过委托调用协议方法dataLoading时,它只是不识别它——给出了一个预期的标识符错误

以下是协议/接口文件:

#import <Foundation/Foundation.h>

@class LoaderView;

@protocol DataLoaderProtocol <NSObject>

@required
- (void) dataLoading;
- (void) doneLoading;

@end

@interface DataLoader : NSObject {

}

@property (retain) id <DataLoaderProtocol> delegate;
@property (retain, nonatomic) LoaderView *loader;

- (id) initWithDelegate: (id <DataLoaderProtocol>) delegate;
- (void) start;

@end
以下是实施文件:

#import "DataLoader.h"
#import "LoaderView.h"


@implementation DataLoader

@synthesize delegate = _delegate;
@synthesize loader = _loader;

- (id) initWithDelegate: (id <DataLoaderProtocol>) delegate
{
    self.delegate = delegate;

    return self;
}

- (void) start
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
                                        initWithTarget:self.delegate
                                        selector:@selector([self.delegate dataLoading]) 
                                        object:nil];
    [queue addOperation:operation]; 
    [operation release];
}

@end
错误出现在此行:选择器:@selector[self.delegate dataload]

我确信这是我犯的一个愚蠢的错误,但我不明白为什么它不识别该方法,因为代理与协议绑定…

您编写selector:@selector[self.delegate dataload]的方式是错误的尝试使用:selector:@selectordataLoading代替。

您编写selector:@selector的方式:@selector[self.delegate dataLoading]错误,请尝试使用:选择器:@selectordataLoading。

调用initWithDelegate时,我不知道是否定义了self。这可能会把下游的事情搞砸

尝试:

我不知道当您调用initWithDelegate时是否定义了self。这可能会把下游的事情搞砸

尝试:


您正在传递一个选择器,即SEL类型,因此需要编写以下代码:

NSInvocationOperation *operation = 
    [[NSInvocationOperation alloc] 
        initWithTarget:self.delegate
              selector:@selector(dataLoading) // the name of the selector here 
                object:nil];

您正在传递一个选择器,即SEL类型,因此需要编写以下代码:

NSInvocationOperation *operation = 
    [[NSInvocationOperation alloc] 
        initWithTarget:self.delegate
              selector:@selector(dataLoading) // the name of the selector here 
                object:nil];

Doh!!是的,你是对的,因为我已经指定了目标!多么愚蠢的错误。Doh!!是的,你是对的,因为我已经指定了目标!多么愚蠢的错误。对,我们不也需要直接赋值吗?delegate=[aDelegate retain];对,我们不也需要直接赋值吗?delegate=[保留遗赠];