shared UIWebView for all views
I want to add a UIWebView to all the view present in the app. I am not getting the web view loaded as well as visible. Can some please help me on the same.
In order to get it working I created a Shared Web view class.
//.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
And in all the view controllers, I am calling the same as
-(UIWebView *) testWebView {
return [WebViewAdds sharedWebView] ;
}
-(void) viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.testWebView];
}
There are two problems that I see.
First is that you are not really doing a singleton correctly. This SO question has some good info in it.
Second problem is that you have a subclass of UIWebView and you are setting its delegate to itself. Ideally, delegates are a separate class that provides extra behavior.
精彩评论