代码之家  ›  专栏  ›  技术社区  ›  Cesar

@选择器和返回值

  •  0
  • Cesar  · 技术社区  · 15 年前

    这个想法很简单,我有一个http下载类,这个类必须支持http身份验证,但它基本上是一个后台线程,所以我想避免直接向屏幕提示,我想使用一个委托方法来要求从类外,如viewcontroller。

    但我不知道是否可能,或者我是否必须使用不同的语法。

    此类使用此委托协议:

    //Updater.h
    @protocol Updater <NSObject>
    -(NSDictionary *)authRequired;
    @optional
    -(void)statusUpdate:(NSString *)newStatus;
    -(void)downloadProgress:(int)percentage;
    @end
    
    
    
    @interface Updater : NSThread {
    ...
    }
    

    这是对委托方法的调用:

    //Updater.m
    // This check always fails :(
    if ([self.delegate respondsToSelector:@selector(authRequired:)]) { 
        auth = [delegate authRequired];
    }
    

    这是委托方法的实现

    //rootViewController.m
    -(NSDictionary *)authRequired;
    {
        // TODO: some kind of popup or modal view
        NSMutableDictionary *ret=[[NSMutableDictionary alloc] init]; 
        [ret setObject:@"utente" forKey:@"user"];
        [ret setObject:@"password" forKey:@"pass"];
        return ret;
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   kennytm    15 年前
    if ([self.delegate respondsToSelector:@selector(authRequired:)]) { 
    

    在objc,冒号( : )在方法名中是重要的。那意味着 authRequired authRequired: 是不同的方法。请改为:

    if ([delegate respondsToSelector:@selector(authRequired)]) {