1

我想向委托方法添加一个参数(来自 NSXMLParserDelegate)

这是迄今为止的方法:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    // save the characters for the current item...
    if ([string   isEqual: @"off"]) {
         myObject.isON = NO; //doesn't know what is myObject
    }

我想要的是 :

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string:(MyObject*)anObject{
    // save the characters for the current item...
    if ([string   isEqual: @"off"]) {
         anObject.isON = NO;
    }

谢谢

4

1 回答 1

1

首先,您需要子类化您的NSXMLParser,添加新delegate属性调用它subclassDelegate或类似的东西,这样您就可以区分超类的委托。在 init 中成为您的超类的代表self.delegate = self

响应委托方法并将您不想覆盖的方法转发到self.subclassDelegate响应您要覆盖的方法并在子类协议中覆盖它。

这是示例:

    @protocol  MyXMLParserDelegate;
@interface MyXMLParser : NSXMLParser<NSXMLParserDelegate>
@property (weak) id<MyXMLParserDelegate> subclassDelegate;
@end

@protocol  MyXMLParserDelegate <NSObject>
- (void)parserDidStartDocument:(NSXMLParser *)parser;
// this is the method that you override
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string withObject:(id)object;
@end

然后在.m

@implementation MyXMLParser

- (id)init
{
    self = [super init];
    if(self) {
        self.delegate = self;
    }
    return self;
}


#pragma mark - repspond to NSXMLParser delegate

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [self.subclassDelegate parser:parser foundCharacters:string withObject:yourObject];
}
于 2014-02-27T12:23:22.287 回答