0

我有一个 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 'UIWebViewDelegate

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

4

2 回答 2

1

你只是忘记UIWebViewDelegate了你的类定义:

#import <UIKit/UIKit.h>

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

@end
于 2012-10-05T18:35:26.140 回答
1

除了 FX 答案,我想你忘了在 viewDidAppear 中添加这个(在 loadRequest 之后):

[self.view addSubview:theWebView];

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

于 2012-10-05T18:53:35.883 回答