【问题标题】:Using Webview within Class Method在类方法中使用 Webview
【发布时间】:2012-11-14 07:31:43
【问题描述】:

基本上我有一个类方法,我正在调用它来调用 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 方法中执行?由于在类方法中,每当我分配一个委托时,它都会产生一个我无法将其分配给自身的错误。

谢谢。

【问题讨论】:

    标签: iphone objective-c ios ipad cocoa


    【解决方案1】:

    您可以将您的类的静态实例作为委托,并从那里处理整个事情。 还记得在你的 .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;
    }
    

    【讨论】:

    • 伊斯梅尔,这不是和辛格尔顿一样吗?谢谢。顺便说一句,这是一个好方法。我只是好奇,如果我可以在 dispatch_async 中创建实例为“webview”并加载 webview,一旦加载我就可以返回视图。
    【解决方案2】:

    由于在类方法中,每当我分配一个委托时,它都会生成一个 我无法将其分配给自己的错误。

    只是一个快速的想法。为什么不编写一个新类并将其对象分配为委托而不是自身?

    【讨论】:

    • 我很好奇如何做到这一点?有什么与这种想法相关的例子吗?
    猜你喜欢
    • 2015-07-09
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2016-05-07
    • 2017-10-30
    • 1970-01-01
    相关资源
    最近更新 更多