iPhone多視圖開發(fā)案例紀(jì)實
本文是iPhone多視圖開發(fā)和WebService客戶端技術(shù)實現(xiàn)的一個案例介紹。文中以一個簡單的例子來說明iPhone多視圖開發(fā)。
1.新建iPhone項目
打開XCode,新建IPhone項目,選擇“window-based Application”模板,項目名稱暫定為shouji138,效果如下圖:
完成之后的界面如下圖:
2.添加控制視圖的ViewController類:SwitchViewController;
這里用一個ViewController來負(fù)責(zé)整個程序的視圖跳轉(zhuǎn),這樣控制起來就很方便了。
在XCode左邊“Classes”上面點右鍵,“Add”->“New File...”,選擇“Cocoa Touch Class”->“UIViewController subclass”,取名為SwitchViewController,如下圖:
3.添加第一個視圖的控制類FirstViewController
添加第一個視圖控制類,命名為FirstViewController,方法同上,如下圖:
4.添加第一個視圖,F(xiàn)irstView.xib
添加視圖文件,在Resources上面新建文件,選擇“User Interface”->“View XIB”,輸入名稱“FirstView”,如下圖:
#p#
5.連接好FirstView的對應(yīng)關(guān)系
添加了視圖控制類和XIB文件之后,需要將他們關(guān)聯(lián)起來。方法如下:
雙擊新建的“FirstView.xib”文件,打開界面設(shè)計器,選擇"FirstView.xib"的屬性面板,選中“File's Owner”,如下圖:
選中菜單“Tools”->“Inspector”,調(diào)出屬性窗口,選中最后一個標(biāo)簽欄,在“Class”下,選中“FirstViewController”,如下圖:
在第二個標(biāo)簽欄,選中“Outlets”下的view,用鼠標(biāo)拖曳它到FirstView.xib屬性窗口中的“View”上面,如下圖:
6.在FirstView.xib上添加控件
選擇菜單“Tools”->“Library”,調(diào)出控件庫,拖一個Label和Button到設(shè)計窗口,效果如下圖:
7.添加第二個視圖:SecondViewController和SecondView.xib
如法炮制添加第二個視圖,方法同上。
8.連接好SecondView的對應(yīng)關(guān)系
如法炮制連接好ViewController和View。
9.在SecondView.xib添加控件
如法炮制添加好控件,如下圖。
10.在控制類SwitchViewController添加代碼,實現(xiàn)對2個視圖的跳轉(zhuǎn)。
在SwitchViewController.h中添加代碼:
- //
- // SwitchViewController.h
- // shouji138.com 手機(jī)主題
- //
- // Created by administrator on 8/27/09.
- // Copyright 2009 __MyCompanyName__. All rights reserved.
- //
- #import
- @class FirstViewController;
- @class SecondViewController;
- @interface SwitchViewController : UIViewController {
- FirstViewController* firstviewcontroller;
- SecondViewController* secondviewcontroller;
- }
- @property (nonatomic,retain) FirstViewController* firstviewcontroller;
- @property (nonatomic,retain) SecondViewController* secondviewcontroller;
- -(void)initView;
- -(void)showFirstView;
- -(void)showSecondView;
- -(void)removeAllView;
- @end
- 說明一下:
- initView 方法用來程序加載時初始化view,showFirstView方法用來顯示第一個view,showSecondView用來顯示第二view。
- 在SwitchViewController.m中添加代碼:
- //
- // SwitchViewController.m
- // shouji138.com 手機(jī)主題
- //
- // Created by administrator on 8/27/09.
- // Copyright 2009 __MyCompanyName__. All rights reserved.
- //
- #import "SwitchViewController.h"
- #import "FirstViewController.h"
- #import "SecondViewController.h"
- @implementation SwitchViewController
- @synthesize firstviewcontroller;
- @synthesize secondviewcontroller;
- -(void)initView{
- NSLog(@"ttt");
- if(self.firstviewcontroller == nil){
- self.firstviewcontroller = [[FirstViewController alloc]initWithNibName:@"FirstView" bundle:nil];
- }
- [self removeAllView];
- [self.view insertSubview:self.firstviewcontroller.view atIndex:0];
- }
- -(void)showFirstView{
- if(self.firstviewcontroller == nil){
- self.firstviewcontroller = [[FirstViewController alloc]initWithNibName:@"FirstView" bundle:nil];
- }
- [self removeAllView];
- [self.view insertSubview:self.firstviewcontroller.view atIndex:0];
- }
- -(void)showSecondView{
- if(self.secondviewcontroller == nil){
- self.secondviewcontroller = [[SecondViewController alloc]initWithNibName:@"SecondView" bundle:nil];
- }
- [self removeAllView];
- [self.view insertSubview:self.secondviewcontroller.view atIndex:0];
- }
- -(void)removeAllView{
- for(NSInteger i=0;i<[self.view.subviews count];i++){
- [[self.view.subviews objectAtIndex:i] removeFromSuperview];
- }
- }
- /*
- // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
- // Custom initialization
- }
- return self;
- }
- */
- /*
- // Implement loadView to create a view hierarchy programmatically, without using a nib.
- - (void)loadView {
- }
- */
- /*
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- */
- /*
- // Override to allow orientations other than the default portrait orientation.
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- */
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (void)dealloc {
- [firstviewcontroller release];
- [secondviewcontroller release];
- [super dealloc];
- }
- @end
#p#
11.修改shouji138AppDelegate代碼
修改shouji138AppDelegate.h,代碼如下:
- //
- // shouji138AppDelegate.h
- // shouji138.com 手機(jī)主題
- //
- // Created by administrator on 8/27/09.
- // Copyright __MyCompanyName__ 2009. All rights reserved.
- //
- #import
- @class SwitchViewController;
- @interface shouji138AppDelegate : NSObject
{ - IBOutlet UIWindow *window;
- IBOutlet SwitchViewController *viewController;
- }
- @property (nonatomic, retain) UIWindow *window;
- @property (nonatomic, retain) SwitchViewController *viewController;
- +(shouji138AppDelegate *)App;
- @end
- 修改shouji138AppDelegate.m代碼如下:
- //
- // shouji138AppDelegate.m
- // shouji138.com 手機(jī)主題下載
- //
- // Created by administrator on 8/27/09.
- // Copyright __MyCompanyName__ 2009. All rights reserved.
- //
- #import "shouji138AppDelegate.h"
- #import "SwitchViewController.h"
- @implementation shouji138AppDelegate
- @synthesize window;
- @synthesize viewController;
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
- // Override point for customization after application launch
- [window addSubview:viewController.view];
- [viewController initView];
- [window makeKeyAndVisible];
- }
- +(shouji138AppDelegate *)App{
- return (shouji138AppDelegate *)[[UIApplication sharedApplication]delegate];
- }
- - (void)dealloc {
- [window release];
- [viewController release];
- [super dealloc];
- }
- @end
其中:applicationDidFinishLaunching 方法中調(diào)用了SwitchViewController的initView方法,把第一個視圖FirstView加載到了屏幕中,因此程序運(yùn)行之后,我們看到的第一個頁面是FirstView。
選擇菜單“Build”->“Build”,進(jìn)行編譯,如果沒有問題,應(yīng)該可以編譯通過。
12.在MainWindow.xib中連接好與SwitchViewController的對應(yīng)關(guān)系。
這一步是非常重要的。
雙擊“MainWindow.xib”,調(diào)出“Interface Builder”;
從Library控件庫中,拖動一個view Controller到“MainWindow.xib”窗口;
將這個添加的view Controller的Class設(shè)置為SwitchViewController;
選擇“Shouji138 APP Delegate”,在“Outlets”->“viewController”中,拖曳一個連接線到“Switch View Controller”;
到此,完成了最重要的部分了,保存之后,點擊“Build and Go”,應(yīng)該會出現(xiàn)第一個頁面。
13.添加FirstViewController和SecondViewController代碼
修改FirstViewController.h如下:
- //
- // FirstViewController.h
- // shouji138.com
- //
- // Created by administrator on 8/27/09.
- // Copyright 2009 __MyCompanyName__. All rights reserved.
- //
- #import
- @interface FirstViewController : UIViewController {
- }
- -(IBAction)buttonClick:(id)sender;
- @end
- 修改FirstViewController.m如下
- //
- // FirstViewController.m
- // shouji138.com
- //
- // Created by administrator on 8/27/09.
- // Copyright 2009 __MyCompanyName__. All rights reserved.
- //
- #import "FirstViewController.h"
- #import "shouji138AppDelegate.h"
- #import "SwitchViewController.h"
- @implementation FirstViewController
- -(IBAction)buttonClick:(id)sender{
- [[shouji138AppDelegate App].viewController showSecondView];
- }
- ....中間省略.....
- - (void)dealloc {
- [super dealloc];
- }
- @end
- 修改SecondViewController.h如下:
- //
- // SecondViewController.h
- // shouji138.com
- //
- // Created by administrator on 8/27/09.
- // Copyright 2009 __MyCompanyName__. All rights reserved.
- //
- #import
- @interface SecondViewController : UIViewController {
- }
- -(IBAction)buttonClick:(id)sender;
- @end
- 修改SecondViewController.m如下:
- //
- // SecondViewController.m
- // shouji138.com
- //
- // Created by administrator on 8/27/09.
- // Copyright 2009 __MyCompanyName__. All rights reserved.
- //
- #import "SecondViewController.h"
- #import "shouji138AppDelegate.h"
- #import "SwitchViewController.h"
- @implementation SecondViewController
- -(IBAction)buttonClick:(id)sender{
- [[shouji138AppDelegate App].viewController showFirstView];
- }
- ....中間省略.....
- - (void)dealloc {
- [super dealloc];
- }
- @end
編譯一下。
14.連接輸出口
雙擊“FirstView.xib”,進(jìn)入“Interface Builder”,選擇“Show Second”按鈕,選擇“Button Connections”->“Events”->“Touch Up Inside”,拖出連接線到“File's Owner”,選擇輸出口“buttonClick”,效果如下圖:
按照同樣的設(shè)置,將SecondView.xib的“Show First”按鈕事件連接到SecondViewController的buttonClick方法。
15.運(yùn)行調(diào)試
點擊“Build and Go”,在模擬器上出現(xiàn)第一個頁面,點擊“Show Second”按鈕,跳轉(zhuǎn)到第二個頁面,點擊“Show First”按鈕,跳轉(zhuǎn)到第一個頁面。
本文來自haolinks的博客:《iPhone多視圖開發(fā)》
【編輯推薦】