您的位置:首页 > 编程语言

实例iPhone编程入门教程-第十三天

2011-09-22 15:16 246 查看
DAY thirteen – My Google



今天来建立一个 iPhone app 软件,用你的iPhone 直接打开谷歌网页。

纲要:

- 在程序显示前运行代码 -

- 关于iPhone的“UIWebView” 运用 -

首先运行以安装好的 xCode

选择: File->New Project.

从 "New Project" 窗口

选择 : iPhone OS ->Applications-> Utility Application

命名 : 我这里命名为 “Mygoogle”

(1) 在xCode打开 MainViewController.h 文件,加入下面代码

@interface MainViewController : UIViewController {

IBOutlet UIWebView *webView;

}

@end

(2) 在xCode打开 MainViewController.m 文件,加入下面代码

#import "MainViewController.h"

#import "MainView.h"

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

// Custom initialization

}

return self;

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

// set userInteractionEnabled to true so the user can click links

webView.userInteractionEnabled = true;

// navigate to google

[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"http://www.google.com"]]];

}

// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview

// Release anything that's not essential, such as cached data

}

- (void)dealloc {

[super dealloc];

}

(3) 在xCode打开 FlipsdeViewController.h 文件,加入下面代码

#import <UIKit/UIKit.h>

@interface FlipsideViewController : UIViewController {

IBOutlet UIWebView *webView;

}

@end

(4) 在xCode打开 FlipsdeViewController.m 文件,加入下面代码

#import "FlipsideViewController.h"

@implementation FlipsideViewController

- (void)viewDidLoad {

webView.userInteractionEnabled = true;

// load our about page from the resource bundle

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html"];

NSData* htmlData = [NSData dataWithContentsOfFile:filePath];

[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://www.mywebsite.com/about.html"]];

}

// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview

// Release anything that's not essential, such as cached data

}

- (void)dealloc {

[super dealloc];

}

@end

(5) 导入下面html文件

下载下面html,放入 Mygoogle 文件夹内并命名为下面名称about.html

文件:

about.html
在xCode下右键点击 Mygoogle->Add->Existing Files; 在 Mygoogle 文件夹内,选择下载好的图片,按 Add

(6) MainView 界面设置

双击文件: "mainView.xib" ;

然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面

(7) 加入 UIWebView ; 显示:www.google.com

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Webview 到 Main View

在主视窗口或文件窗口;点击 UIWebView

选择: Tools -> Connection Inspector

移动鼠标在"New Referencing Outlets" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";

放开鼠标选择键出现 "webView"; 选上它。

(8) FlipsideView 界面设置

双击文件: "mainView.xib" ;

然后 "Interface Builder" 会自动打开,在这里我们可以编辑改变界面

(9) 加入 UIWebView ; 显示:about.html

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Webview 到 Main View

在主视窗口或文件窗口;点击 UIWebView

选择: Tools -> Connection Inspector

移动鼠标在"New Referencing Outlets" 后面圆圈上; 圆圈变为(+); 拖向直线连接到"File's Owner";

放开鼠标选择键出现 "webView"; 选上它。

最后在 xCode 选择 Build->Build and Go; Save All.

下载今天教程文件:

Mygoogle.zip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: