0

我尝试加载与委托关联的 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];
}

我自动调整 web 视图的大小并将它们添加到NSMutableArray调用的webviews. 但是,webViewDidFinishLoad不叫。

在头.h文件中,接口定义为:

@interface ViewController : UIViewController<UIWebViewDelegate>

我错过了什么?web_view循环中的是否被处置?

ps 它看起来像这个问题的副本,但事实并非如此。


替代方法 1

宣布于.h

@property (nonatomic, weak) NSMutableArray *webviews;

然后执行:

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

替代方法 2

我没有实例化UIWebViewin for-loop,而是将其放在头文件中。

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

然后更改for循环:

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

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


总结与亮点:

我的目标:

  1. 加载所有UIWebView可变大小的内容
  2. Web 视图应自动适应内容的大小(无需滚动);这就是为什么webViewDidFinishLoad需要。
  3. 在当前视图(或可能是一个)上正确排列 Web 视图UIScrollView,以使其不重叠。
4

3 回答 3

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 视图在加载时正确调用委托。

于 2013-08-06T04:56:35.030 回答
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];
}
于 2013-08-06T06:43:31.137 回答
0

如果您使用 ARC,则 web_view 在 for 循环结束时被释放。

于 2013-08-06T04:04:38.410 回答