UIWebView委托:未调用webViewDidFinishLoad

时间:2013-08-06 04:02:55

标签: objective-c uiwebview uiwebviewdelegate

我尝试加载一个UIWebView数组,并附带委托。

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);
}

其中msg是从XML读取的一些HTML代码。 XML已正确加载(由NSLog行验证)。然后在我的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];
}

我会自动调整网络视图的大小,并将其添加到名为NSMutableArray的{​​{1}}。但是,webviews未被调用。

在标题webViewDidFinishLoad文件中,接口定义为:

.h

我错过了什么?循环中的@interface ViewController : UIViewController<UIWebViewDelegate> 是否被处理?

P.S。它看起来像是question的副本,但事实并非如此。


替代方法1

web_view声明:

.h

然后实施:

@property (nonatomic, weak) NSMutableArray *webviews;

替代方法2

我没有在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); } 中实例化UIWebView,而是将其放在头文件中。

for-loop

然后更改for循环:

@interface ViewController : UIViewController<UIWebViewDelegate> {
    UIWebView *web_view;
}

在这种方法中,只调用最后一条消息的委托。


摘要&amp;亮点:

我的目标:

  1. 加载具有可变大小内容的所有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]; }
  2. 网页视图应自动适合内容的大小(不滚动);这就是为什么UIWebView是必需的。
  3. 在当前视图(或可能是webViewDidFinishLoad)上正确排列网页视图,以使其不重叠。

3 个答案:

答案 0 :(得分:1)

你应该这样做,

@interface UIViewController <UIWebViewDelegate>
{

}
@property(nonatomic, strong)NSMutableArray *webViews;

@end

////////////////

@implementation UIViewController

-(void)viewDidLoad
{
  [super viewDidLoad];

   webViews = [[NSMutableArray alloc] init];

   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];

     //i don't know why would  you not add these web-views on the view controller,
     // but still, here is the addition of these.
     [self.view addSubView:web_view];
    }
}

@end

这应该使web-views在加载时正确调用委托。

答案 1 :(得分:0)

如果您正在使用ARC,则会在for循环结束时取消分配web_view。

答案 2 :(得分:0)

这是我的最终解决方案(希望它对其他人有用):

在Storyboard中,我在视图控制器中添加了UIScrollView,并将其与IBOutlet相关联。

标题文件:

#import <UIKit/UIKit.h>
#import "GDataXMLNode.h"

@interface ViewController : UIViewController<UIWebViewDelegate> {
    IBOutlet UIScrollView *scroll_view;
}
@property (nonatomic, strong) UIWebView *web_view;

@end

实施文件:

float currentY;
NSArray *array;
int count;
GDataXMLDocument *doc;

- (void)viewDidLoad
{
    [super viewDidLoad];

    currentY = 0;
    count = 0;
    [self loadXML];
}

- (void)loadXML {
   // Some codes to load the XML contents into array variable
   [self loadWebView];
}

- (void)loadWebView {
    if(count < array.count) {
        GDataXMLElement *post = [array objectAtIndex:count];
        NSString *msg = [[[post elementsForName:@"message"] objectAtIndex:0] stringValue];
        count++;
        self.web_view = [[UIWebView alloc] initWithFrame:CGRectMake(10, currentY, 300, 1.0f)];
        self.web_view.delegate = self;
        [self.web_view setHidden:YES];
        [self.web_view loadHTMLString:msg baseURL:nil];
        [scroll_view addSubview:self.web_view];
    } else {
        // end the process
        [scroll_view setContentSize:CGSizeMake(320, currentY + 30)];
        return;
    }
}

- (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;
    [webView setHidden:NO];

    currentY += webView.frame.size.height + 20.0f; // 20.0f is spaces between posts

    NSLog(@"WebView Height: %.1f", webView.frame.size.height);
    [self loadWebView];
}