007 在Xcode4.5上创建IOS6.0应用 (网页视图控件 WEBVIEW)

网页视图控件  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打印出来的百度的源代码



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shuaiyinoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值