iphone - 所有 View 共享 UIWebView

标签 iphone cocoa-touch ios uikit uiwebview

我想向应用程序中存在的所有 View 添加一个 UIWebView。我没有加载和显示 Web View 。有人可以帮助我吗?

为了让它工作,我创建了一个共享 Web View 类。

//.h Implementation
@interface WebViewAdds : UIWebView {

}

+ (WebViewAdds *) sharedWebView ;
@end


WebViewAdds *g_sharedWebView ;

@implementation WebViewAdds

+(WebViewAdds *) sharedWebView  
{
    if (g_sharedWebView == nil) {

        g_sharedWebView = [[WebViewAdds alloc] initWithFrame:CGRectMake(0, 400, 320, 60)];

        [g_sharedWebView setDelegate:self];

        NSString *urlAddress = @"http://www.google.com";
        NSURL *url = [NSURL URLWithString:urlAddress];

        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

        [g_sharedWebView loadRequest:requestObj];
    }

    return g_sharedWebView;
}

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}



- (void)dealloc {
    [super dealloc];
}

@end

在所有的 View Controller 中,我调用的是和

-(UIWebView *) testWebView {

    return [WebViewAdds sharedWebView] ;
}

-(void) viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.testWebView];   
}

最佳答案

我看到了两个问题。

首先是你没有真正正确地做单例。 This SO question里面有一些很好的信息。

第二个问题是你有一个 UIWebView 的子类并且你正在将它的委托(delegate)设置为它自己。理想情况下,委托(delegate)是提供额外行为的单独类。

关于iphone - 所有 View 共享 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5225623/

相关文章:

ios - 无法从 Swift 继承 NSInputStream(initWithData : unrecognizer selector)

iphone - 为 iPhone 和 iPad 构建应用程序

iphone - 编辑时如何将图像叠加添加到UIImagePickerControllerSourceTypePhotoLibrary?

iphone - 触摸时淡出 UIButton

objective-c - 比较两个数组并将相等的对象放入新数组中

ios - 创建XMPPRoom时如何设置和获取roomName(组名)?

iphone - iOS 崩溃报告 : Random objects receiving the wrong selector

iphone - 波兰语的 CFStringEncoding 是什么?

iphone - 点击tabBar时刷新tableView

ios - rightBarButtonItem,leftBarButtonItem 问题