iOS - UIWebView不显示HTML

时间:2012-10-05 18:31:36

标签: html ios ios5 uiviewcontroller uiwebview

我有一个UIViewController,其中我放了一个UIWebView

然后我将其添加到.h文件中:

#import <UIKit/UIKit.h>

@interface BuildBusinessController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *theWebView;

@end

并将其作为.m文件

#import "BuildBusinessController.h"

@interface BuildBusinessController ()

@end

@implementation BuildBusinessController
@synthesize theWebView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    theWebView.delegate = self;
}

- (void)viewDidUnload
{
    [self setTheWebView:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)viewDidAppear:(BOOL)animated
{  
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"build_business" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:htmlFile];
    NSURLRequest *rq = [NSURLRequest requestWithURL:url];
    [theWebView loadRequest:rq];
}

@end

但是build_business.html文件没有在视图中呈现,而且我在这一行上收到警告:theWebView.delegate = self;其中说:

从不兼容的类型BuildBusinessController * const_string

分配给id&#39; UIWebViewDelegate

有人知道我在做错了什么吗?我觉得我忘记了一件小事:)

2 个答案:

答案 0 :(得分:1)

您只是忘记了UIWebViewDelegate类定义:

#import <UIKit/UIKit.h>

@interface BuildBusinessController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *theWebView;

@end

答案 1 :(得分:1)

除了F.X.回答,我想你忘了在viewDidAppear中添加这个(在loadRequest之后):

[self.view addSubview:theWebView];

更新:问题是由于OP未能在Storyboard中为UIWebView配置委托而引起的。此处提供的代码段仅在您手动编码时才有效。否则,正如@ F.X所述。在下面的评论中,Storyboard将自动实现这一点。