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
- # Import
- # Import
- @ Interface MainViewController: UIViewController {
- UITextView * textView;
- }
- // The default initialization function uses init instead of initWithFrame.
- -(Id) init;
- -(Void) dealloc;
- // The system will call loadView to arrange your own sub-view
- -(Void) loadView;
- @ 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.
- (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) Of course, you can replace self. view with your own view.
- (void)loadView {
- [ super loadView ];
- CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];
- textView = [ [ UITextView alloc ] initWithFrame: bounds ];
- textView.text = @"Hello, World! ";
- 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.
- MainViewController *myViewController = [
- [ MainViewController alloc ]
- initWithNibName: @"MainViewController"
- bundle: nil
- ];
2.3 change direction
(1) The system checks whether it can be rotated to the direction indicated by interfaceOrientation through shouldAutorotateToInterfaceOrientation.
- (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) When the direction changes, the system will call didRotateFromInterfaceOrientation
- (void)didRotateFromInterfaceOrientation:
- (UIInterfaceOrientation)fromInterfaceOrientation
- {
- }
2.4 clear 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;
- }
Summary: DetailsIPhone SDKDevelopmentUIKitI hope this article will be helpful to you.