1

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];   
}
Pintu
  • 369
  • 2
  • 6
  • 18
  • [g_sharedWebView setDelegate:self]; looks unnecessary unless you did implement delegate methods. – Seyther Mar 07 '11 at 23:07

1 Answers1

1

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.

Community
  • 1
  • 1
Simon Goldeen
  • 9,080
  • 3
  • 36
  • 45