在类方法中使用Webview

时间:2012-11-26 19:02:12

标签: iphone objective-c ios ipad cocoa

基本上我有一个类方法,我在调用机制中调用Web服务调用,当我得到响应时,我在同步模式下以NSDictionary发送响应。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{

    NSDictionary *_pD= [HttpRequest Details:@"Type" :@"guest" :[Description valueForKey:@"number"]];

    dispatch_sync(dispatch_get_main_queue(), ^{

        DLog(@"%@",[_D description]);
        [self mapObjects:_D];

    });
});

但是,在这个调用中我还需要调用WebView,并等待从webview调用javascript,因此我很好奇是否可以在Class方法中执行?因为在类方法中,无论何时我分配了一个委托,它都会产生一个我无法将其分配给自己的错误。

感谢。

2 个答案:

答案 0 :(得分:0)

您可以让您的类的静态实例作为委托,并从那里处理整个事物。 另请记住在.h

中设置UIWebViewDelegate协议
+ (YourClass *)getInstance {
    static YourClass *instance = nil;
    if (!instance) {
        instance = [YourClass new];
    }
    return instance;
}

- (void)callWebService {
    ... do your thing
}

并且这样打电话:

[[YourClass getInstance] callWebService];

如果你想对getInstance方法感到挑剔,那么正确的方法是这样做(我不想让你混淆奇怪的代码):

+ (YourClass *)getInstance {
    static dispatch_once_t pred;
    static YourClass *instance = nil;

    dispatch_once(&pred, ^{
        instance = [[[self class] alloc] init];
    });

    return instance;
}

答案 1 :(得分:0)

  

因为在类方法中,只要我分配了一个委托,它就会生成一个   错误,我无法将其分配给自己。

快速思考。为什么不编写新类并将其对象分配为委托而不是自己?