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

通过objective-c选择器回调

  •  2
  • codemonkey  · 技术社区  · 16 年前

    [[self delegate] performSelectorOnMainThread:@selector(bsRequestFinished:) withObject:self waitUntilDone:YES];
    

    ... 其中“bsRequestFinished”是调用视图控制器中的回调方法。这一切都很好地工作,直到我意识到,有些网页将需要作出不同类型的请求。。。i、 我想在回调函数中做不同类型的事情,这取决于请求的类型。

    [[self delegate] performSelectorOnMainThread:@selector(self.variableCallbackFunctionName) withObject:self waitUntilDone:YES];
    

    这有可能吗?如果是,明智吗?如果不是,其他选择?

    EDIT:请注意,无论我得到什么修复,都需要考虑这个类正在发出异步请求的现实。。。因此,我需要确保回调函数处理与特定请求正确关联。。。因为我不能依赖FIFO处理序列。

    编辑2:看起来像传入SEL类型的变量是一种方法吗?我现在要做的是找出将其保存为BSjax类的保留属性的语法。

    2 回复  |  直到 12 年前
        1
  •  2
  •   Tom H    16 年前

    variBkecallbackfunctionname是什么类型的变量?一根绳子?如果是,你可能会想要这样的东西:

    [[self delegate] performSelectorOnMainThread:NSSelectorFromString(self.variableCallbackFunctionName) withObject:self waitUntilDone:YES];
    

    我在用iPad,所以我不能测试这个,但是试试看。

    编辑:如果variableCallbackFunctionName不是一个nsstring,最简单的方法可能是,如果我理解你的问题的话。

    编辑2: http://developer.apple.com/mac/library/iPad/index.html#documentation/cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html%23//apple_ref/c/func/NSSelectorFromString

        2
  •  3
  •   Chuck    16 年前

    SEL oneSelector = @selector(bsRequestFinished:);
    SEL anotherselector = @selector(somethingElse:);
    NSString *nameOfSelector = askUserForSelectorName();
    SEL dynamicSelector = NSSelectorFromString(nameOfSelector);
    

    那就做你的决定吧 variableCallbackFunctionName 这比你想象的要简单得多:

    [[self delegate] performSelectorOnMainThread:self.variableCallbackFunctionName withObject:self waitUntilDone:YES];