《iPhone與iPad開發實戰—iOS經典應用剖析》連載八
3.3.5 主視圖控製器代碼主視圖控製器是MainViewController,由於視圖使用的控件都已經在視圖MainView中定義了,所以在視圖控製器MainViewController代碼很少了,在本應用中還設計了按鈕按下和按鈕選擇時候的普通和高亮狀態效果。這些效果可以在檢查器中設定,也可以通過代碼設定,本應用是通過代碼設定這些效果。
先看看主視圖控製器類MainViewController,它的h文件定義請參考“代碼清單3-5Password/Classes/MainViewController.h”所示。
【代碼清單3-1】 Password/Classes/MainViewController.h
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController { IBOutletUIButton *createPassword; IBOutletUIButton *emailPassword; } @property (nonatomic, retain) UIButton*createPassword; @property (nonatomic, retain) UIButton*emailPassword; @end
createPassword是Generate Password對應的控件變量,emailPassword 是E-Mail Password對應的控件變量,它們都定義了輸出口,這是因為它們都需要在程序中改變控件狀態。
主視圖控製器類MainViewController,它的m文件定義請參考“代碼清單3-6Password/Classes/ MainViewController.m”所示。
【代碼清單3-2】 Password/Classes/ MainViewController.m
#import "MainViewController.h" #import "MainView.h" @implementation MainViewController @synthesize createPassword; @synthesize emailPassword; - (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil { if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { } returnself; } - (void)viewDidLoad { UIImage*buttonBackground = [[UIImage imageNamed:@"blueButton.png"]stretchableImageWithLeftCapWidth:12.0 topCapHeight:12.0 ]; [createPasswordsetBackgroundImage:buttonBackground forState:UIControlStateNormal]; [emailPasswordsetBackgroundImage:buttonBackground forState:UIControlStateNormal]; [createPasswordsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [emailPasswordsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; UIImage*buttonBackgroundSel = [[UIImage imageNamed:@"whiteButton.png"]stretchableImageWithLeftCapWidth:12.0 topCapHeight:12.0 ]; [createPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateHighlighted]; [createPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateSelected]; [emailPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateHighlighted]; [emailPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateSelected]; [createPasswordsetTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; [createPasswordsetTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; [emailPasswordsetTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; [emailPasswordsetTitleColor:[UIColor blackColor] forState:UIControlStateSelected]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ //Return YES for supported orientations return(interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)didReceiveMemoryWarning { [superdidReceiveMemoryWarning]; // Releases the view if it doesn't have a superview //Release anything that's not essential, such as cached data } - (void)dealloc { [emailPasswordrelease]; [createPasswordrelease]; [superdealloc]; } @end
其中viewDidLoad方法是我們討論的重點,其中通過下麵的方法定義了一個UIImage對象:
UIImage *buttonBackground = [[UIImageimageNamed:@"blueButton.png"] stretchableImageWithLeftCapWidth:12.0topCapHeight:12.0 ];
該方法是通過拉伸創建一個UIImage,而邊角不拉伸,需要兩個參數,第一個是不拉伸區域和左邊框的寬度,第二個參數是不拉伸區域和上邊框的寬度。
把這個拉伸的UIImage對象作為兩個按鈕的正常狀態時候背景圖片:
[createPassword setBackgroundImage:buttonBackgroundforState:UIControlStateNormal];
[emailPassword setBackgroundImage:buttonBackgroundforState:UIControlStateNormal];
接下來又定義了一個UIImage對象,作為按鈕其它狀態(高亮狀態UIControlStateHighlighted和選中狀態UIControlStateSelected)時候的背景圖片,
[createPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateHighlighted];
[createPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateSelected];
然後,又定義了按鈕在高亮狀態和選中狀態時候的文字背景顏色:
[createPassword setTitleColor:[UIColor blackColor]forState:UIControlStateHighlighted];
[createPassword setTitleColor:[UIColor blackColor]forState:UIControlStateSelected];
3.3.6 背後視圖UI
背後麵視圖如圖3-43所示是FlipsideView(背後視圖)設計窗口,我們一步一步介紹如何實現該視圖設計和編程。

背後視圖中的控件進行了編號,視圖中的控件內容見表3-8所示。
表3-8 FlipsideView視圖中的控件
編號 |
控件項目 |
控件類型 |
1 |
Check out our other applications. |
UILabel |
2 |
圖片控件 |
UIImageView |
3 |
iFlame is a virtual lighter… |
UITextView |
4 |
Download |
UIButton |
5 |
Don't be caught without a light… |
UITextView |

我們可以按照表3-8一一添加這些控製,需要注意的是1號控件是UILabel,而3和5號控件是UITextView,當有很多的文本內容需要顯示的時候就要使用UITextView控件而不是UILabel。這個視圖設計過程細節就不再一一介紹了。
3.6.7 背後麵視圖和視圖控製器代碼
背後視圖主要實現了2個功能:導航欄中的Done按鈕和視圖中Download按鈕,其中導航欄中的Done功能的實現是在RootViewController.m類的toggleView方法中已經實現了,而不是在FlipsideView.m或FlipsideViewController.m中實現的。
Download按鈕是通過瀏覽器打開在App Store上一個iFlame應用,該功能是在FlipsideView.m中實現的。
我們先看看FlipsideView.h代碼請參考“代碼清單3-7 Password/Classes/ FlipsideView.h”所示。
【代碼清單3-1】 Password/Classes/ FlipsideView.h
#import <UIKit/UIKit.h>
@interface FlipsideView : UIView {
}
- (IBAction)openLink;
@end
其中的openLink方法是響應Download按鈕事件。FlipsideView.m代碼請參考“代碼清單3-8 Password/Classes/ FlipsideView.m”所示。
【代碼清單3-2】 Password/Classes/ FlipsideView.m
#import "FlipsideView.h" @implementation FlipsideView - (id)initWithFrame:(CGRect)frame { if(self = [super initWithFrame:frame]) { //Initialization code } returnself; } - (void)drawRect:(CGRect)rect { //Drawing code } - (void)dealloc { [superdealloc]; } -(IBAction) openLink { //open in Safari //[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"https://www.apple.com/"]]; [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"https://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=287545019&mt=8"]]; } @end
上麵的代碼主要的方法是openLink,通過該方法在iOS瀏覽器中打開一個網頁。其中使用[UIApplication sharedApplication] openURL:方法,該方法介紹E-Mail發送功能時候已經介紹了,它可以打開多種應用程序。
本章小結
通過對本章的學習,讀者可以掌握密碼生成應用程序(Amuck Password Generator)應用開發過程,重點是一些基本控件設計和使用過程,這些控件包括:UIView、UIButton和UILabel等,學會使用Interface Builder,在Interface Builder設計這些控件,設定它們的屬性。
讀者還可以了解MVC設計模式、實用型應用程序模板等概念,Cocoa和Cocoa Touch中MVC設計模式最為重要的設計模式,隻有能夠真正的理解好MVC設計模式,才能做好iOS開發,才能理解nib文件、視圖和視圖控製器這些概念。UIView級別動畫是iOS比較簡單但很常用的動畫,UIView級別動畫必須放在[UIView beginAnimations:nilcontext:NULL]和[UIViewcommitAnimations]語句之間,其中包括了設定動畫持續時間、動畫轉變類型和動畫曲線等動畫屬性的設定。
此外,讀者還可以掌握[UIApplication sharedApplication] openURL:方法的使用,iOS中這個[UIApplication sharedApplication]openURL方法可以做很多事情,其中包括:打開瀏覽器、打開Google地圖、撥打電話、發送短信和發送Email等等。
最後更新:2017-04-02 16:47:37