3分钟教你做一个iphone手机浏览器
第一步:新建一个Single View工程:
第二步:新建好工程,关闭arc。
第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的title 属性设置为 http:// 。UIButton 的title属性设置为 go 。 布局如图:
第四步:为Text Field 和 UIWebView 连线,插座变量分别命名为 textUrl 和 webRequest。为UIButton 连线 .连接一个action事件(- (IBAction)btnGo:(id)sender;)
然后在(- (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
整个项目代码如下:
// // wsqViewController.h // webTest // // Created by administrator on 13-9-5. // Copyright (c) 2013年 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> @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
分类: Objective-C