詳解iPhone SDK 開發(fā)之 UIKit 使用
詳解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
- #import
- #import
- @interface MainViewController : UIViewController {
- UITextView *textView;
- }
- //默認的初始化函數(shù)用init,而不是initWithFrame
- - (id)init;
- - (void)dealloc;
- //系統(tǒng)會調(diào)用loadView來安排你自己的子view
- - (void)loadView;
- @end
(2) UIViewController會自動創(chuàng)建一個UIView對象 self.view, 你可以把自己的view添加到這個self.view里去,例如下面的例子:垂直顯示兩個text view.
- (void)loadView {
- CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];
- textView1 = [ [ UITextView alloc ] initWithFrame:
- CGRectMake(0, 0, bounds.size.width, bounds.size.height / 2)
- ];
- textView2 = [ [ UITextView alloc ] initWithFrame:
- CGRectMake(0, bounds.size.height / 2,
- bounds.size.width,
- bounds.size.height / 2)
- ];
- textView1.text = @"Hello, World!";
- textView2.text = @"Hello again!";
- [ self.view addSubview: textView1 ];
- [ self.view addSubview: textView2 ];
- }
(3)當然你也可以把self.view整個替換成自己的view
- (void)loadView {
- [ super loadView ];
- CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];
- textView = [ [ UITextView alloc ] initWithFrame: bounds ];
- textView.text = @"Hello, World! ";
- self.view = textView;
- }
(4)一般loadView只會被調(diào)用一次, 但是當內(nèi)存不夠用的時候,
UIViewController會調(diào)用didReceiveMemoryWarning方法, 你可以在這個方法里釋放自己的資源, 然后loadView會被重新自動調(diào)用.
2.2 使用interface builder
你可以用UIViewController類的initWithNibName方法加載interface builder創(chuàng)建的.xib資源文件.
- MainViewController *myViewController = [
- [ MainViewController alloc ]
- initWithNibName: @"MainViewController"
- bundle: nil
- ];
2.3 方向改變
(1)系統(tǒng)通過shouldAutorotateToInterfaceOrientation來檢查是否可以旋轉(zhuǎn)到interfaceOrientation所指示的方向.
- (BOOL)shouldAutorotateToInterfaceOrientation:
- (UIInterfaceOrientation)interfaceOrientation
- {
- return (YES);
- }
- UIDeviceOrientationUnknown //Catchall for errors or hardware failures
- UIDeviceOrientationPortrait //Oriented upright vertically in portrait mode
- UIDeviceOrientationPortraitUpsideDown //Oriented upside-down vertically in portrait mode
- UIDeviceOrientationLandscapeLeft //Device is rotated counter-clockwise in landscape mode
- UIDeviceOrientationLandscapeRight //Device is rotated clockwise in landscape mode
- UIDeviceOrientationFaceUp //Device is laying flat, face up, such as on a table
- UIDeviceOrientationFaceDown //Device is laying flat, face down, such as on a table
(2)當方向改變時,系統(tǒng)會調(diào)用didRotateFromInterfaceOrientation
- (void)didRotateFromInterfaceOrientation:
- (UIInterfaceOrientation)fromInterfaceOrientation
- {
- }
2.4 清除view controller
- (void)dealloc {
- [ textView release ];
- [ super dealloc ];
- }
2.5 Controller demo
- Example 3-7. ControllerDemo application delegate prototypes (ControllerDemoAppDelegate.h)
- #import
- @class ControllerDemoViewController;
- @interface ControllerDemoAppDelegate : NSObject {
- UIWindow *window;
- ControllerDemoViewController *viewController;
- }
- @property (nonatomic, retain) IBOutlet UIWindow *window;
- @property (nonatomic, retain) IBOutlet ControllerDemoViewController *viewController;
- @end
- Example 3-8. ControllerDemo application delegate (ControllerDemoAppDelegate.m)
- #import "ControllerDemoAppDelegate.h"
- #import "ControllerDemoViewController.h"
- @implementation ControllerDemoAppDelegate
- @synthesize window;
- @synthesize viewController;
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
- CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];
- self.window = [ [ [ UIWindow alloc ] initWithFrame: screenBounds ]
- autorelease
- ];
- viewController = [ [ ControllerDemoViewController alloc ] init ];
- [ window addSubview:viewController.view ];
- [ window makeKeyAndVisible ];
- }
- - (void)dealloc {
- [viewController release];
- [window release];
- [super dealloc];
- }
- @end
- Example 3-9. ControllerDemo view controller prototype (ControllerDemoViewController.h)
- #import
- #import
- @interface ControllerDemoViewController : UIViewController {
- NSString *helloWorld, *woahDizzy;
- UITextView *textView;
- }
- @end
- Example 3-10. ControllerDemo view controller (ControllerDemoViewController.m)
- #import "ControllerDemoViewController.h"
- @implementation ControllerDemoViewController
- - (id)init {
- self = [ super init ];
- if (self != nil) {
- helloWorld = [ [ NSString alloc ] initWithString: @"Hello, World!" ];
- woahDizzy = [ [ NSString alloc ] initWithString: @"Woah, I'm Dizzy!" ];
- }
- return self;
- }
- - (void)loadView {
- [ super loadView ];
- textView = [ [ UITextView alloc ] initWithFrame:
- [ [ UIScreen mainScreen ] applicationFrame ]
- ];
- textView.text = helloWorld;
- self.view = textView;
- }
- -(BOOL)shouldAutorotateToInterfaceOrientation:
- (UIInterfaceOrientation)interfaceOrientation
- {
- return YES;
- }
- - (void)didRotateFromInterfaceOrientation:
- (UIInterfaceOrientation)fromInterfaceOrientation
- {
- textView.text = woahDizzy;
- }
- - (void)viewDidLoad {
- [ super viewDidLoad ];
- }
- - (void)didReceiveMemoryWarning {
- [ super didReceiveMemoryWarning ];
- }
- - (void)dealloc {
- [ helloWorld release ];
- [ woahDizzy release ];
- [ textView release ];
- [ super dealloc ];
- }
- @end
- Example 3-11. ControllerDemo main (main.m)
- #import
- int main(int argc, char *argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- int retVal = UIApplicationMain(argc, argv, nil, @"ControllerDemoAppDelegate");
- [pool release];
- return retVal;
- }
小結(jié):詳解iPhone SDK 開發(fā)之 UIKit 使用的內(nèi)容介紹完了希望本文對你有所幫助。