I try to load an array of UIWebView with delegate associated.
for (GDataXMLElement *post in array) {
NSString *msg = [[[post elementsForName:@"message"] objectAtIndex:0] stringValue];
UIWebView *web_view = [[UIWebView alloc] initWithFrame:CGRectZero];
web_view.delegate = self;
[web_view loadHTMLString:msg baseURL:nil];
NSLog(@"Msg: %@", msg);
}
where msg
is some HTML codes reading from XML. XML is loaded properly (verified by the NSLog
line). Then in my webViewDidFinishLoad:
:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(@"WebView Height: %.1f", webView.frame.size.height);
[webviews addObject:webView];
}
I auto resize the web views and add them to a NSMutableArray
called webviews
. However, webViewDidFinishLoad
is not called.
In the header .h
file, the interface is defined as:
@interface ViewController : UIViewController<UIWebViewDelegate>
What did I miss? Is the web_view
in the loop get disposed ?
p.s. It looks like a duplicate of this question, but it isn't.
Alternate Approach 1
Declared at .h
:
@property (nonatomic, weak) NSMutableArray *webviews;
Then for implementation:
for (GDataXMLElement *post in array) {
NSString *msg = [[[post elementsForName:@"message"] objectAtIndex:0] stringValue];
UIWebView *web_view = [[UIWebView alloc] initWithFrame:CGRectZero];
web_view.delegate = self;
[web_view loadHTMLString:msg baseURL:nil];
NSLog(@"Msg: %@", msg);
[self.webviews addObject:web_view];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(@"WebView Height: %.1f", webView.frame.size.height);
}
Alternate Approach 2
Instead of instantiating UIWebView
in for-loop
, I put it in header file.
@interface ViewController : UIViewController<UIWebViewDelegate> {
UIWebView *web_view;
}
Then change the for-loop:
for (GDataXMLElement *post in array) {
NSString *msg = [[[post elementsForName:@"message"] objectAtIndex:0] stringValue];
web_view = [[UIWebView alloc] initWithFrame:CGRectZero];
web_view.delegate = self;
[web_view loadHTMLString:msg baseURL:nil];
NSLog(@"Msg: %@", msg);
[self.webviews addObject:web_view];
}
In this approach, only the delegate of last message gets called.
Summary & Highlights:
My objectives:
- Load all
UIWebView
with variable-size contents - The web views should auto fit the size of contents ( without scrolling ); that's why
webViewDidFinishLoad
is required. - Arrange the web views properly on current view ( or probably a
UIScrollView
) in order to make it not overlapped.