我想初始化UIWebview
内的WebViewController
对象。我在初始化它时遇到了一些麻烦,并将其指向从之前ViewController
发送给它的URL。
在我viewDidLoad
的{{1}}中,我尝试使用以下内容对其进行初始化:
WebViewController
但是,这给我错误说明:
UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:urlRequest];
_myWebView.delegate=self;
[WebViewController loadRequest:urlRequest];
和use of undeclared identifier'theFrame'
。
完整代码如下:
WebViewController.h:
no known class method for selector loadRequest
WebViewController.m:
#import <UIKit/UIKit.h>
#import "MatchCenterViewController.h"
@interface WebViewController : UIViewController
@property (strong, nonatomic) NSURL *itemURL;
@property (weak, nonatomic) IBOutlet UIWebView *myWebView;
@end
答案 0 :(得分:1)
首先,您需要定义您将.h文件中的UIWebView Delegate用作:
@interface WebViewController : UIViewController <UIWebViewDelegate>
如果您已在Xib文件中定义了webView,为什么要创建另一个UIWebView并将其分配给IBOutlet的webView?你可以这样做:
self.myWebView.delegate = self; //Note that I have set the delegate first before calling LoadRequest
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView setScalesPageToFit:YES];
[self.myWebView loadRequest:request];