详解iPhone SDK 开发之 UIKit 使用

移动开发 iOS
UIKit是iPhone的用户界面框架,和传统的OS X程序的AppKit相类似。其中多数类都与它们以NS打头的Appkit中的内容近似。先来看内容。

详解iPhone SDK 开发之 UIKit 使用是本文要介绍的内容,关于UIKit,你可以使用UIKit框架来建立和管理iPhone应用程序的用户界面。这个Objective-C框架特别为Multi-Touch界面提供了一个应用程序对象、事件处理、绘图模型、窗口、视图和控件。

UIKit使用2

2. View Controllers

可以使用UIViewController类来创建和显示多个view, 就像前一个例子里MainView来控制TextView一样.

UIViewController还提供旋转(例如横握或竖握你的iphone)你的view,或低内存报警等功能.

2.1 创建一个view controller

(1)从UIViewController继承一个自己的view controller

  1.   #import   
  2.   #import   
  3.   @interface MainViewController : UIViewController {  
  4.   UITextView *textView;  
  5.   }  
  6.   //默认的初始化函数用init,而不是initWithFrame  
  7.   - (id)init;  
  8.   - (void)dealloc;  
  9.   //系统会调用loadView来安排你自己的子view  
  10.   - (void)loadView;  
  11.   @end 

(2) UIViewController会自动创建一个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只会被调用一次, 但是当内存不够用的时候,
  
UIViewController会调用didReceiveMemoryWarning方法, 你可以在这个方法里释放自己的资源, 然后loadView会被重新自动调用.

2.2 使用interface builder

你可以用UIViewController类的initWithNibName方法加载interface builder创建的.xib资源文件.

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

2.3 方向改变

(1)系统通过shouldAutorotateToInterfaceOrientation来检查是否可以旋转到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)当方向改变时,系统会调用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.   } 

小结:详解iPhone SDK 开发之 UIKit 使用的内容介绍完了希望本文对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-18 09:44:33

iPhone SDK仪表控件UIDialView

2011-08-18 09:52:13

iPhone SDKUIPageContr

2011-08-18 10:02:47

iPhone SDKOpenFlow

2011-07-06 17:53:40

iPhone SDK Xcode

2011-07-06 17:48:30

iPhone Xcode 模拟器

2011-08-17 15:19:38

iPhone应用数据

2011-08-18 10:59:57

iPhone开发消息通信NSNotificat

2011-07-06 17:40:43

iPhone SDK

2011-07-18 09:35:29

iPhone 框架

2011-08-02 13:46:43

iPhone开发 iPhone SDK

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-07-22 18:25:20

XCode iPhone SDK

2011-07-27 10:16:41

iPhone SQLite 数据库

2011-08-17 15:10:21

iPhone开发Web视图

2011-07-29 15:47:21

iPhone开发 Objective- C

2011-08-16 17:28:49

iPhone SDK正则表达式

2011-05-12 08:49:58

iPhone SDKXcode

2010-01-28 10:31:32

Android使用SD

2011-07-20 15:20:14

IPhone AVAudioRec

2011-08-01 18:27:58

iPhone开发 UISearchBa
点赞
收藏

51CTO技术栈公众号