我想在我的程序中显示网页,我在我的IB(.xib)中使用WebView对象,但它有效,是否有任何样本代码,而不是文档或文章?
view_w.h
@interface view_w : NSObject {
IBOutlet WebView* Web;
}
-(IBAction) google:(id) sender;
@end
view_w.m
-(IBAction) google:(id) sender
{
NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[Web loadrequest:request];
}
// error No -loadrequest:找到方法
答案 0 :(得分:2)
您应该将webview连接到视图控制器类的插座。然后
NSURL *url = [NSURL URLWithString:@"http://yoursite.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[yourwebviewoutlet loadRequest:request];
就是这样。
编辑以取消有关未找到方法的警告,请正确指定类型:
IBOutlet UIWebView * web; // and use lowercase for variables while you're at it.
答案 1 :(得分:1)
// view.h
#import <Cocoa/Cocoa.h>
#import<WebKit/WebKit.h>
@interface view : NSObject {
IBOutlet WebView* webview;
}
-(IBAction) google:(id) sender;
@end
// view.m
#import "view.h"
@implementation view
-(IBAction) google:(id) sender
{
[[webview mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.google.com/finance/converter"]]];
}
@end