作者:Aaron Chen | 来源:互联网 | 2023-09-07 13:58
原文链接:http:www.cnblogs.comwsq724439564p3302805.html第一步:新建一个SingleView工程:第二步:新建好工程,关闭arc。第三步
原文链接:http://www.cnblogs.com/wsq724439564/p/3302805.html
第一步:新建一个Single View工程:
![](https://images0.cnblogs.com/blog/474990/201309/05103225-ef7b105006954e6fbc2d48a4883e93ba.x-png)
第二步:新建好工程,关闭arc。
![](https://images0.cnblogs.com/blog/474990/201309/05103338-a482404a1ae446679b358166100438b3.x-png)
第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的title 属性设置为 http:// 。UIButton 的title属性设置为 go 。 布局如图:
![](https://images0.cnblogs.com/blog/474990/201309/05103750-d3a652d11dae4442a0e8dabf938dbecd.x-png)
第四步:为Text Field 和 UIWebView 连线,插座变量分别命名为 textUrl 和 webRequest。为UIButton 连线 .连接一个action事件(- (IBAction)btnGo:(id)sender;)
![](https://images0.cnblogs.com/blog/474990/201309/05104321-eb81c85cae8e447a8047ae2e5e6457cb.x-png)
然后在(- (IBAction)btnGo:(id)sender;里面添加代码:
- (IBAction)btnGo:(id)sender {
NSURL* url = [[NSURL alloc] initWithString:textUrl.text];
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
[webRequest loadRequest:request];
[textUrl resignFirstResponder];
[url release];
[request release];
}
到此。一个简单浏览器设计就完成了 。运行如下:输入 http://sina.com.cn
![](https://images0.cnblogs.com/blog/474990/201309/05105126-8ae224494f7d446db1e0811541badb01.x-png)
整个项目代码如下:
//
// wsqViewController.h
// webTest
//
// Created by administrator on 13-9-5.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
#import
@interface wsqViewController : UIViewController {
UITextField *textUrl;
UIWebView *webRequest;
}
@property (retain, nonatomic) IBOutlet UITextField *textUrl;
@property (retain, nonatomic) IBOutlet UIWebView *webRequest;
- (IBAction)btnGo:(id)sender;
@end
//
// wsqViewController.m
// webTest
//
// Created by administrator on 13-9-5.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
#import "wsqViewController.h"
@implementation wsqViewController
@synthesize textUrl;
@synthesize webRequest;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setTextUrl:nil];
[self setWebRequest:nil];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[textUrl release];
[webRequest release];
[super dealloc];
}
- (IBAction)btnGo:(id)sender {
NSURL* url = [[NSURL alloc] initWithString:textUrl.text];
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
[webRequest loadRequest:request];
[textUrl resignFirstResponder];
[url release];
[request release];
}
@end
转载于:https://www.cnblogs.com/wsq724439564/p/3302805.html