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

FXの回答に加えて、これをviewDidAppearに追加するのを忘れたと思います(loadRequestの後):

[self.view addSubview:theWebView];

更新:この問題は、OPがストーリーボードでUIWebViewのデリゲートを構成できないことが原因で発生します。ここで提供されるコードスニペットは、手動でコーディングしている場合にのみ機能します。それ以外の場合は、以下のコメントで@FXが示すように、Storyboardはこれを自動的に実装します。

于 2012-10-05T18:53:35.883 に答える
1

UIWebViewDelegateクラス定義を忘れているだけです:

#import <UIKit/UIKit.h>

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

@end
于 2012-10-05T18:35:26.140 に答える