为所有视图共享UIWebView

时间:2011-03-07 21:42:21

标签: iphone cocoa-touch ios uikit uiwebview

我想在应用程序中的所有视图中添加UIWebView。我没有加载Web视图以及可见。有些人可以帮助我。

为了使其正常工作,我创建了一个共享Web视图类。

//.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

在所有视图控制器中,我调用

-(UIWebView *) testWebView {

    return [WebViewAdds sharedWebView] ;
}

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

1 个答案:

答案 0 :(得分:1)

我看到有两个问题。

首先,你并没有真正正确地做单身人士。 This SO question有一些很好的信息。

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

相关问题