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

詳解iPhone SDK 開發(fā)之 UIKit 使用

移動開發(fā) iOS
UIKit是iPhone的用戶界面框架,和傳統(tǒng)的OS X程序的AppKit相類似。其中多數(shù)類都與它們以NS打頭的Appkit中的內(nèi)容近似。先來看內(nèi)容。

詳解iPhone SDK 開發(fā)之 UIKit 使用是本文要介紹的內(nèi)容,關(guān)于UIKit,你可以使用UIKit框架來建立和管理iPhone應(yīng)用程序的用戶界面。這個Objective-C框架特別為Multi-Touch界面提供了一個應(yīng)用程序?qū)ο?、事件處理、繪圖模型、窗口、視圖和控件。

UIKit使用2

2. View Controllers

可以使用UIViewController類來創(chuàng)建和顯示多個view, 就像前一個例子里MainView來控制TextView一樣.

UIViewController還提供旋轉(zhuǎn)(例如橫握或豎握你的iphone)你的view,或低內(nèi)存報警等功能.

2.1 創(chuàng)建一個view controller

(1)從UIViewController繼承一個自己的view controller

  1.   #import   
  2.   #import   
  3.   @interface MainViewController : UIViewController {  
  4.   UITextView *textView;  
  5.   }  
  6.   //默認的初始化函數(shù)用init,而不是initWithFrame  
  7.   - (id)init;  
  8.   - (void)dealloc;  
  9.   //系統(tǒng)會調(diào)用loadView來安排你自己的子view  
  10.   - (void)loadView;  
  11.   @end 

(2) UIViewController會自動創(chuàng)建一個UIView對象 self.view, 你可以把自己的view添加到這個self.view里去,例如下面的例子:垂直顯示兩個text view.

  1.    (void)loadView {  
  2.   CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];  
  3.   textView1 = [ [ UITextView alloc ] initWithFrame:  
  4.   CGRectMake(0, 0, bounds.size.width, bounds.size.height / 2)  
  5.   ];  
  6.   textView2 = [ [ UITextView alloc ] initWithFrame:  
  7.   CGRectMake(0, bounds.size.height / 2,  
  8.   bounds.size.width,  
  9.   bounds.size.height / 2)  
  10.   ];  
  11.   textView1.text = @"Hello, World!";  
  12.   textView2.text = @"Hello again!";  
  13.   [ self.view addSubview: textView1 ];  
  14.   [ self.view addSubview: textView2 ];  
  15.   } 

(3)當然你也可以把self.view整個替換成自己的view

  1.   (void)loadView {  
  2.   [ super loadView ];  
  3.   CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];  
  4.   textView = [ [ UITextView alloc ] initWithFrame: bounds ];  
  5.   textView.text = @"Hello, World! ";  
  6.   self.view = textView;  
  7.   } 

(4)一般loadView只會被調(diào)用一次, 但是當內(nèi)存不夠用的時候,
  
UIViewController會調(diào)用didReceiveMemoryWarning方法, 你可以在這個方法里釋放自己的資源, 然后loadView會被重新自動調(diào)用.

2.2 使用interface builder

你可以用UIViewController類的initWithNibName方法加載interface builder創(chuàng)建的.xib資源文件.

  1.   MainViewController *myViewController = [  
  2.   [ MainViewController alloc ]  
  3.   initWithNibName: @"MainViewController"  
  4.   bundle: nil  
  5.   ]; 

2.3 方向改變

(1)系統(tǒng)通過shouldAutorotateToInterfaceOrientation來檢查是否可以旋轉(zhuǎn)到interfaceOrientation所指示的方向.

  1.   (BOOL)shouldAutorotateToInterfaceOrientation:  
  2.   (UIInterfaceOrientation)interfaceOrientation  
  3.   {  
  4.   return (YES);  
  5.   }  
  6.   UIDeviceOrientationUnknown //Catchall for errors or hardware failures  
  7.   UIDeviceOrientationPortrait //Oriented upright vertically in portrait mode  
  8.   UIDeviceOrientationPortraitUpsideDown //Oriented upside-down vertically in portrait mode  
  9.   UIDeviceOrientationLandscapeLeft //Device is rotated counter-clockwise in landscape mode  
  10.   UIDeviceOrientationLandscapeRight //Device is rotated clockwise in landscape mode  
  11.   UIDeviceOrientationFaceUp //Device is laying flat, face up, such as on a table  
  12.   UIDeviceOrientationFaceDown //Device is laying flat, face down, such as on a table 

(2)當方向改變時,系統(tǒng)會調(diào)用didRotateFromInterfaceOrientation

  1.   (void)didRotateFromInterfaceOrientation:  
  2.   (UIInterfaceOrientation)fromInterfaceOrientation  
  3.   {  
  4.   } 

2.4 清除view controller

  1.   (void)dealloc {  
  2.   [ textView release ];  
  3.   [ super dealloc ];  
  4.   } 

2.5 Controller demo  

  1.   Example 3-7. ControllerDemo application delegate prototypes (ControllerDemoAppDelegate.h)  
  2.   #import   
  3.   @class ControllerDemoViewController;  
  4.   @interface ControllerDemoAppDelegate : NSObject {  
  5.   UIWindow *window;  
  6.   ControllerDemoViewController *viewController;  
  7.   }  
  8.   @property (nonatomic, retain) IBOutlet UIWindow *window;  
  9.   @property (nonatomic, retain) IBOutlet ControllerDemoViewController *viewController;  
  10.   @end  
  11.   Example 3-8. ControllerDemo application delegate (ControllerDemoAppDelegate.m)  
  12.   #import "ControllerDemoAppDelegate.h"  
  13.   #import "ControllerDemoViewController.h"  
  14.   @implementation ControllerDemoAppDelegate  
  15.   @synthesize window;  
  16.   @synthesize viewController;  
  17.   - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  18.   CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];  
  19.   self.window = [ [ [ UIWindow alloc ] initWithFrame: screenBounds ]  
  20.   autorelease  
  21.   ];  
  22.   viewController = [ [ ControllerDemoViewController alloc ] init ];  
  23.   [ window addSubview:viewController.view ];  
  24.   [ window makeKeyAndVisible ];  
  25.   }  
  26.   - (void)dealloc {  
  27.   [viewController release];  
  28.   [window release];  
  29.   [super dealloc];  
  30. }  
  31.   @end  
  32.   Example 3-9. ControllerDemo view controller prototype (ControllerDemoViewController.h)  
  33.   #import   
  34.   #import   
  35.   @interface ControllerDemoViewController : UIViewController {  
  36.   NSString *helloWorld, *woahDizzy;  
  37.   UITextView *textView;  
  38.   }  
  39.   @end  
  40.   Example 3-10. ControllerDemo view controller (ControllerDemoViewController.m)  
  41.   #import "ControllerDemoViewController.h"  
  42.   @implementation ControllerDemoViewController  
  43.   - (id)init {  
  44.   self = [ super init ];  
  45.   if (self != nil) {  
  46.     
  47.   helloWorld = [ [ NSString alloc ] initWithString: @"Hello, World!" ];  
  48.   woahDizzy = [ [ NSString alloc ] initWithString: @"Woah, I'm Dizzy!" ];  
  49.   }  
  50.   return self;  
  51.   }  
  52.   - (void)loadView {  
  53.   [ super loadView ];  
  54.   textView = [ [ UITextView alloc ] initWithFrame:  
  55.   [ [ UIScreen mainScreen ] applicationFrame ]  
  56.   ];  
  57.   textView.text = helloWorld;  
  58.   self.view = textView;  
  59.   }  
  60.   -(BOOL)shouldAutorotateToInterfaceOrientation:  
  61.   (UIInterfaceOrientation)interfaceOrientation  
  62.   {  
  63.   return YES;  
  64.   }  
  65.   - (void)didRotateFromInterfaceOrientation:  
  66.   (UIInterfaceOrientation)fromInterfaceOrientation  
  67.   {  
  68.   textView.text = woahDizzy;  
  69.   }  
  70.   - (void)viewDidLoad {  
  71.   [ super viewDidLoad ];  
  72.   }  
  73.   - (void)didReceiveMemoryWarning {  
  74.   [ super didReceiveMemoryWarning ];  
  75.   }  
  76.   - (void)dealloc {  
  77.   [ helloWorld release ];  
  78.   [ woahDizzy release ];  
  79.   [ textView release ];  
  80.   [ super dealloc ];  
  81.   }  
  82.   @end  
  83.   Example 3-11. ControllerDemo main (main.m)  
  84.   #import   
  85.  
  86.   int main(int argc, char *argv[]) {  
  87.   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  88.   int retVal = UIApplicationMain(argc, argv, nil, @"ControllerDemoAppDelegate");  
  89.   [pool release];  
  90.   return retVal;  
  91.   } 

小結(jié):詳解iPhone SDK 開發(fā)之 UIKit 使用的內(nèi)容介紹完了希望本文對你有所幫助。

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-18 09:44:33

iPhone SDK儀表控件UIDialView

2011-08-18 10:02:47

iPhone SDKOpenFlow

2011-08-18 09:52:13

iPhone SDKUIPageContr

2011-07-06 17:53:40

iPhone SDK Xcode

2011-07-06 17:48:30

iPhone Xcode 模擬器

2011-08-18 10:59:57

iPhone開發(fā)消息通信NSNotificat

2011-08-17 15:19:38

iPhone應(yīng)用數(shù)據(jù)

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-07-18 09:35:29

iPhone 框架

2011-07-06 17:40:43

iPhone SDK

2011-08-02 13:46:43

iPhone開發(fā) iPhone SDK

2011-07-27 10:16:41

iPhone SQLite 數(shù)據(jù)庫

2011-08-17 15:10:21

iPhone開發(fā)Web視圖

2011-07-22 18:25:20

XCode iPhone SDK

2011-07-29 15:47:21

iPhone開發(fā) Objective- C

2011-08-16 17:28:49

iPhone SDK正則表達式

2011-05-12 08:49:58

iPhone SDKXcode

2011-07-20 15:20:14

IPhone AVAudioRec

2010-01-28 10:31:32

Android使用SD

2011-08-10 10:10:21

iPhoneUIPopoverCo
點贊
收藏

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