网页视图控件 WEBVIEW
关于网页视图控件的使用直接上代码,因为在代码里面写好了有关控件使用时要注意的事项
ViewController.h
@interface ViewController : UIViewController <UIWebViewDelegate>{
IBOutlet UITextField * myTextFiedld;
IBOutlet UIWebView * myWebView;
}
@property (nonatomic,retain)UITextField * myTextFiedld;
@property (nonatomic,retain)UIWebView * myWebView;
-(IBAction)changeLocation:(id)sender;
@end
ViewController.m
@implementation ViewController
@synthesize myTextFiedld;
@synthesize myWebView;
//Button点击事件
-(IBAction)changeLocation:(id)sender{
myWebView.delegate = self;//注意:一定要设置委托
[myTextFiedld resignFirstResponder];//放弃第一响应者
//通过输入的文字构建URL对象
NSURL * url = [NSURL URLWithString:myTextFiedld.text];
//通过URL对象构建响应对象
NSURLRequest *req = [NSURLRequest requestWithURL:url];
//通过响应对象传入WEB视图中
[myWebView loadRequest:req];
}
//浏览器加载完成事件
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"finsh...");
//这个方法可以执行一段JavaScript脚本
NSString * s = [myWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];
NSLog(@"%@",s);
}
//浏览器加载失败事件
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"error...");
//NSError对象以后会常常用到,非常有用。
NSLog(@"%@",[error description]);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[myTextFiedld release];
[myWebView release];
[super dealloc];
}
@end
代码实现效果
后台通过调用JavaScript打印出来的百度的源代码