自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone多視圖開發(fā)案例紀(jì)實

移動開發(fā) iOS
本文介紹了iPhone多視圖開發(fā)的一個案例?;旧线@就是一個項目的完成筆記,希望能和大家一起分享提高。

本文是iPhone多視圖開發(fā)和WebService客戶端技術(shù)實現(xiàn)的一個案例介紹。文中以一個簡單的例子來說明iPhone多視圖開發(fā)。

1.新建iPhone項目

打開XCode,新建IPhone項目,選擇“window-based Application”模板,項目名稱暫定為shouji138,效果如下圖:

新建IPhone項目

完成之后的界面如下圖:

iPhone多視圖開發(fā)

2.添加控制視圖的ViewController類:SwitchViewController;

這里用一個ViewController來負(fù)責(zé)整個程序的視圖跳轉(zhuǎn),這樣控制起來就很方便了。

在XCode左邊“Classes”上面點右鍵,“Add”->“New File...”,選擇“Cocoa Touch Class”->“UIViewController subclass”,取名為SwitchViewController,如下圖:

iPhone多視圖開發(fā)

iPhone多視圖開發(fā)

3.添加第一個視圖的控制類FirstViewController

添加第一個視圖控制類,命名為FirstViewController,方法同上,如下圖:

添加第一個視圖的控制類

4.添加第一個視圖,F(xiàn)irstView.xib

添加視圖文件,在Resources上面新建文件,選擇“User Interface”->“View XIB”,輸入名稱“FirstView”,如下圖:

iPhone多視圖開發(fā)

iPhone多視圖開發(fā)

#p#

5.連接好FirstView的對應(yīng)關(guān)系

添加了視圖控制類和XIB文件之后,需要將他們關(guān)聯(lián)起來。方法如下:

雙擊新建的“FirstView.xib”文件,打開界面設(shè)計器,選擇"FirstView.xib"的屬性面板,選中“File's Owner”,如下圖:

iPhone多視圖開發(fā)

選中菜單“Tools”->“Inspector”,調(diào)出屬性窗口,選中最后一個標(biāo)簽欄,在“Class”下,選中“FirstViewController”,如下圖:

iPhone多視圖開發(fā)

在第二個標(biāo)簽欄,選中“Outlets”下的view,用鼠標(biāo)拖曳它到FirstView.xib屬性窗口中的“View”上面,如下圖:

iPhone多視圖開發(fā)

6.在FirstView.xib上添加控件

選擇菜單“Tools”->“Library”,調(diào)出控件庫,拖一個Label和Button到設(shè)計窗口,效果如下圖:

iPhone多視圖開發(fā)

7.添加第二個視圖:SecondViewController和SecondView.xib

如法炮制添加第二個視圖,方法同上。

8.連接好SecondView的對應(yīng)關(guān)系

如法炮制連接好ViewController和View。

9.在SecondView.xib添加控件

如法炮制添加好控件,如下圖。

iPhone多視圖開發(fā)

10.在控制類SwitchViewController添加代碼,實現(xiàn)對2個視圖的跳轉(zhuǎn)。

在SwitchViewController.h中添加代碼:

  1. //
  2. // SwitchViewController.h
  3. // shouji138.com 手機(jī)主題
  4. //
  5. // Created by administrator on 8/27/09.
  6. // Copyright 2009 __MyCompanyName__. All rights reserved.
  7. //
  8. #import
  9. @class FirstViewController;
  10. @class SecondViewController;
  11. @interface SwitchViewController : UIViewController {
  12. FirstViewController* firstviewcontroller;
  13. SecondViewController* secondviewcontroller;
  14. }
  15. @property (nonatomic,retain) FirstViewController* firstviewcontroller;
  16. @property (nonatomic,retain) SecondViewController* secondviewcontroller;
  17. -(void)initView;
  18. -(void)showFirstView;
  19. -(void)showSecondView;
  20. -(void)removeAllView;
  21. @end
  22. 說明一下:
  23. initView 方法用來程序加載時初始化view,showFirstView方法用來顯示第一個view,showSecondView用來顯示第二view。
  24. 在SwitchViewController.m中添加代碼:
  25. //
  26. // SwitchViewController.m
  27. // shouji138.com 手機(jī)主題
  28. //
  29. // Created by administrator on 8/27/09.
  30. // Copyright 2009 __MyCompanyName__. All rights reserved.
  31. //
  32. #import "SwitchViewController.h"
  33. #import "FirstViewController.h"
  34. #import "SecondViewController.h"
  35. @implementation SwitchViewController
  36. @synthesize firstviewcontroller;
  37. @synthesize secondviewcontroller;
  38. -(void)initView{
  39. NSLog(@"ttt");
  40. if(self.firstviewcontroller == nil){
  41. self.firstviewcontroller = [[FirstViewController alloc]initWithNibName:@"FirstView" bundle:nil];
  42. }
  43. [self removeAllView];
  44. [self.view insertSubview:self.firstviewcontroller.view atIndex:0];
  45. }
  46. -(void)showFirstView{
  47. if(self.firstviewcontroller == nil){
  48. self.firstviewcontroller = [[FirstViewController alloc]initWithNibName:@"FirstView" bundle:nil];
  49. }
  50. [self removeAllView];
  51. [self.view insertSubview:self.firstviewcontroller.view atIndex:0];
  52. }
  53. -(void)showSecondView{
  54. if(self.secondviewcontroller == nil){
  55. self.secondviewcontroller = [[SecondViewController alloc]initWithNibName:@"SecondView" bundle:nil];
  56. }
  57. [self removeAllView];
  58. [self.view insertSubview:self.secondviewcontroller.view atIndex:0];
  59. }
  60. -(void)removeAllView{
  61. for(NSInteger i=0;i<[self.view.subviews count];i++){
  62. [[self.view.subviews objectAtIndex:i] removeFromSuperview];
  63. }
  64. }
  65. /*
  66. // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
  67. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  68. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  69. // Custom initialization
  70. }
  71. return self;
  72. }
  73. */
  74. /*
  75. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  76. - (void)loadView {
  77. }
  78. */
  79. /*
  80. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  81. - (void)viewDidLoad {
  82. [super viewDidLoad];
  83. }
  84. */
  85. /*
  86. // Override to allow orientations other than the default portrait orientation.
  87. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  88. // Return YES for supported orientations
  89. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  90. }
  91. */
  92. - (void)didReceiveMemoryWarning {
  93. // Releases the view if it doesn't have a superview.
  94. [super didReceiveMemoryWarning];
  95. // Release any cached data, images, etc that aren't in use.
  96. }
  97. - (void)viewDidUnload {
  98. // Release any retained subviews of the main view.
  99. // e.g. self.myOutlet = nil;
  100. }
  101. - (void)dealloc {
  102. [firstviewcontroller release];
  103. [secondviewcontroller release];
  104. [super dealloc];
  105. }
  106. @end

#p#

11.修改shouji138AppDelegate代碼

修改shouji138AppDelegate.h,代碼如下:

  1. //
  2. // shouji138AppDelegate.h
  3. // shouji138.com 手機(jī)主題
  4. //
  5. // Created by administrator on 8/27/09.
  6. // Copyright __MyCompanyName__ 2009. All rights reserved.
  7. //
  8. #import
  9. @class SwitchViewController;
  10. @interface shouji138AppDelegate : NSObject {
  11. IBOutlet UIWindow *window;
  12. IBOutlet SwitchViewController *viewController;
  13. }
  14. @property (nonatomic, retain) UIWindow *window;
  15. @property (nonatomic, retain) SwitchViewController *viewController;
  16. +(shouji138AppDelegate *)App;
  17. @end
  18. 修改shouji138AppDelegate.m代碼如下:
  19. //
  20. // shouji138AppDelegate.m
  21. // shouji138.com 手機(jī)主題下載
  22. //
  23. // Created by administrator on 8/27/09.
  24. // Copyright __MyCompanyName__ 2009. All rights reserved.
  25. //
  26. #import "shouji138AppDelegate.h"
  27. #import "SwitchViewController.h"
  28. @implementation shouji138AppDelegate
  29. @synthesize window;
  30. @synthesize viewController;
  31. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  32. // Override point for customization after application launch
  33. [window addSubview:viewController.view];
  34. [viewController initView];
  35. [window makeKeyAndVisible];
  36. }
  37. +(shouji138AppDelegate *)App{
  38. return (shouji138AppDelegate *)[[UIApplication sharedApplication]delegate];
  39. }
  40. - (void)dealloc {
  41. [window release];
  42. [viewController release];
  43. [super dealloc];
  44. }
  45. @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”窗口;

iPhone多視圖開發(fā)

將這個添加的view Controller的Class設(shè)置為SwitchViewController;

iPhone多視圖開發(fā)

選擇“Shouji138 APP Delegate”,在“Outlets”->“viewController”中,拖曳一個連接線到“Switch View Controller”;

iPhone多視圖開發(fā)

到此,完成了最重要的部分了,保存之后,點擊“Build and Go”,應(yīng)該會出現(xiàn)第一個頁面。

13.添加FirstViewController和SecondViewController代碼

修改FirstViewController.h如下:

  1. //
  2. // FirstViewController.h
  3. // shouji138.com
  4. //
  5. // Created by administrator on 8/27/09.
  6. // Copyright 2009 __MyCompanyName__. All rights reserved.
  7. //
  8. #import
  9. @interface FirstViewController : UIViewController {
  10. }
  11. -(IBAction)buttonClick:(id)sender;
  12. @end
  13. 修改FirstViewController.m如下
  14. //
  15. // FirstViewController.m
  16. // shouji138.com
  17. //
  18. // Created by administrator on 8/27/09.
  19. // Copyright 2009 __MyCompanyName__. All rights reserved.
  20. //
  21. #import "FirstViewController.h"
  22. #import "shouji138AppDelegate.h"
  23. #import "SwitchViewController.h"
  24. @implementation FirstViewController
  25. -(IBAction)buttonClick:(id)sender{
  26. [[shouji138AppDelegate App].viewController showSecondView];
  27. }
  28. ....中間省略.....
  29. - (void)dealloc {
  30. [super dealloc];
  31. }
  32. @end
  33. 修改SecondViewController.h如下:
  34. //
  35. // SecondViewController.h
  36. // shouji138.com
  37. //
  38. // Created by administrator on 8/27/09.
  39. // Copyright 2009 __MyCompanyName__. All rights reserved.
  40. //
  41. #import
  42. @interface SecondViewController : UIViewController {
  43. }
  44. -(IBAction)buttonClick:(id)sender;
  45. @end
  46. 修改SecondViewController.m如下:
  47. //
  48. // SecondViewController.m
  49. // shouji138.com
  50. //
  51. // Created by administrator on 8/27/09.
  52. // Copyright 2009 __MyCompanyName__. All rights reserved.
  53. //
  54. #import "SecondViewController.h"
  55. #import "shouji138AppDelegate.h"
  56. #import "SwitchViewController.h"
  57. @implementation SecondViewController
  58. -(IBAction)buttonClick:(id)sender{
  59. [[shouji138AppDelegate App].viewController showFirstView];
  60. }
  61. ....中間省略.....
  62. - (void)dealloc {
  63. [super dealloc];
  64. }
  65. @end

編譯一下。

14.連接輸出口

雙擊“FirstView.xib”,進(jìn)入“Interface Builder”,選擇“Show Second”按鈕,選擇“Button Connections”->“Events”->“Touch Up Inside”,拖出連接線到“File's Owner”,選擇輸出口“buttonClick”,效果如下圖:

iPhone多視圖開發(fā)

按照同樣的設(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)到第一個頁面。

iPhone多視圖開發(fā) iPhone多視圖開發(fā)

本文來自haolinks的博客:《iPhone多視圖開發(fā)》

【編輯推薦】

  1. iPhone軟件開發(fā)完美起步
  2. 微軟Bing登陸iPhone 開發(fā)工具包發(fā)布
  3. 利用WPF制作iPhone模擬器
  4. 最貴iPhone應(yīng)用TOP10 第一名售價近千美元
  5. 微軟助力Bing登陸iPhone 開發(fā)封裝器
責(zé)任編輯:yangsai 來源: haolinks的博客
相關(guān)推薦

2011-08-15 18:02:32

iPhone開發(fā)表視圖

2011-08-12 10:16:10

iPhone通訊錄聯(lián)系人

2011-07-29 14:18:46

iPhone開發(fā) 動畫

2011-04-13 12:02:10

.NET藥店系統(tǒng)平臺開發(fā)

2011-08-12 10:04:24

iPhone開發(fā)視圖

2011-07-08 14:51:34

iPhone 視圖

2011-08-15 15:44:46

iPhone開發(fā)PDF

2011-08-18 16:24:44

iPhone開發(fā)圖片

2013-03-22 16:49:12

2011-08-17 15:10:21

iPhone開發(fā)Web視圖

2011-08-19 11:10:31

iPhone應(yīng)用

2011-08-19 10:13:05

iPhone開發(fā)

2011-08-16 15:48:37

iPhone開發(fā)抓圖程序

2011-08-09 14:54:29

iPhoneNSDateanotherDate

2011-08-17 16:23:31

iPhone開發(fā)UIViewContr

2011-08-17 16:12:20

iPhone應(yīng)用程序

2011-08-18 15:24:40

iPhone國際化

2011-08-11 17:32:51

iPhone視圖

2016-07-14 11:16:24

華為

2011-08-12 11:23:47

iPhone窗口視圖
點贊
收藏

51CTO技術(shù)棧公眾號