Detailed description of iPhone SDK development using UIKit

Source: Internet
Author: User

DetailsIPhone SDKDevelopmentUIKitUsage is the content to be introduced in this article.UIKit, You can useUIKitFramework to establish and manageIPhoneApplication User Interface. This Objective-C framework specifically provides an application object, event processing, drawing model, window, view, and control for the Multi-Touch interface.

UIKitUse 2

2. View Controllers

You can use the UIViewController class to create and display multiple views, just like the MainView in the previous example to control TextView.

UIViewController also provides functions such as rotating (for example, holding your iphone horizontally or holding your iphone) your view, or low memory alarm.

2.1 create a view controller

(1) inherit a view controller from UIViewController

 
 
  1. # Import
  2. # Import
  3. @ Interface MainViewController: UIViewController {
  4. UITextView * textView;
  5. }
  6. // The default initialization function uses init instead of initWithFrame.
  7. -(Id) init;
  8. -(Void) dealloc;
  9. // The system will call loadView to arrange your own sub-view
  10. -(Void) loadView;
  11. @ End

(2) UIViewController automatically creates a UIView object self. view. You can add your own view to this self. view. For example, the following example shows two text views vertically.

 
 
  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 ];  

(3) Of course, you can replace self. view with your own 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;  

(4) Generally, loadView is called only once, but when the memory is insufficient,

UIViewController will call the didReceiveMemoryWarning method. You can release your own resources in this method, and loadView will be automatically called again.

2.2 Use interface builder

You can use the initWithNibName method of the UIViewController class to load the. xib resource file created by interface builder.

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

2.3 change direction

(1) The system checks whether it can be rotated to the direction indicated by interfaceOrientation through shouldAutorotateToInterfaceOrientation.

 
 
  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) When the direction changes, the system will call didRotateFromInterfaceOrientation

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

2.4 clear view controller

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

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;  

Summary: DetailsIPhone SDKDevelopmentUIKitI hope this article will be helpful to you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.