IPhone Development Study Notes 007 -- iPhone multi-view development under Xcode4.2 (self-created MainWindow. xib and no M

Source: Internet
Author: User

When you create a Window-based Application project, MainWindow. xib is automatically generated and contains at least one UIApplicationDelegate and one UIWindow object.

However, after Xcode4, when a new project is created, the Project template is greatly changed. There is no Window-based Application, but there is an Empty Application (under iOS-> Application ), now we describe to create a project in this way, and then create a MainWindow. xib is used together, And MainWindow is not used. xib implementation.
In addition, multiple views are implemented using three uiviewcontrollers. One FirstViewController uses FirstView. on the xib design page, click the button above to find the SecondViewController interface. A SecondViewController uses SecondView. the xib design interface also has a button on it and switches to FirstViewController. A SwitchViewController switches the interfaces of the two controllers. SwitchViewController acts as rootViewController, if an application is not started with rootViewController, the following error occurs: "Applications are expected to have a root view controller at the end of application launch". the preceding two methods are as follows:
First: Create MainWindow. xib, with window by default. You must manually add delegate to specify MVTestAppDelegate as the class, add UIViewController, specify SwitchViewController as the class, and then [windowaddSubview: viewController. view]; Add the view of the controller to the window. You do not need to use the code to create and initialize the window and controller view entrusted by the application. These are all done through MainWindow. xib. The second method below is to use the delegate window and controller creation and initialization instead of MainWindow. xib.
Second: The MainWindow. xib implementation in the first method is implemented through the code in Delegate. The details are as follows:

 

CMD + SHIFT + N open the new project window, select Empty Application (a delegate and a window are provided by default), and name it MVTest. For example:

 





 

Create SwitchViewController to inherit UIViewController, remove with XIB for user interface,

Like SwitchViewController, create FirstViewController and SecondViewController, and then create FirstView. xib and Second. xib, for example:

 


A label and a button are added to both xib, for example:



Next, add the following content to the MVTestAppDelegate header file:
@ Property (nonatomic, retain) SwitchViewController * viewController; // Note: IBOutlet in front of window and viewController
+ (MVTestAppDelegate *) App;
And implemented in the *. m file, such:







 

In addition, SwitchViewController. h:


# Import <UIKit/UIKit. h>

 


@ ClassFirstViewController;

@ ClassSecondViewController;

 


@ Interface SwitchViewController: UIViewController {

FirstViewController * firstviewcontroller;

SecondViewController * secondviewcontroller;

}

 


@ Property (nonatomic, retain) FirstViewController * firstviewcontroller;

@ Property (nonatomic, retain) SecondViewController * secondviewcontroller;

 


-(Void) initView;

-(Void) showFirstView;

-(Void) showSecondView;

-(Void) removeAllView;

 


@ End


SwitchViewController. m:

# Import "SwitchViewController. h"

# Import "FirstViewController. h"

# Import "SecondViewController. h"

 


@ Implementation SwitchViewController

@ Synthesize firstviewcontroller;

@ Synthesize secondviewcontroller;

 


-(Void) initView {

NSLog (@ "ttt ");

If (self. firstviewcontroller = nil ){

Self. firstviewcontroller = [[FirstViewControlleralloc] initWithNibName: @ "FirstView" bundle: nil];

}

[SelfremoveAllView];

[Self. viewinsertSubview: self. firstviewcontroller. viewatIndex: 0];

}

 


-(Void) showFirstView {

If (self. firstviewcontroller = nil ){

Self. firstviewcontroller = [[FirstViewControlleralloc] initWithNibName: @ "FirstView" bundle: nil];

}

[SelfremoveAllView];

[Self. viewinsertSubview: self. firstviewcontroller. viewatIndex: 0];

}

 


-(Void) showSecondView {

If (self. secondviewcontroller = nil ){

Self. secondviewcontroller = [[SecondViewControlleralloc] initWithNibName: @ "SecondView" bundle: nil];

}

[SelfremoveAllView];

[Self. viewinsertSubview: self. secondviewcontroller. viewatIndex: 0];

}

 


-(Void) removeAllView {

For (NSInteger I = 0; I <[self. view. subviewscount]; I ++ ){

[[Self. view. subviewsobjectAtIndex: I] removeFromSuperview];

}

}

@ End
In the two controllers, FirstViewController and SecondViewController, add-(IBAction) buttonClick :( id) sender; and implement:
-(IBAction) buttonClick :( id) sender {

 

[[MVTestAppDelegateApp]. viewControllershowSecondView]; // FirstViewController calls showSecondView and showFirstView in SecondViewController.

}
Specify FirstView. the File's Owner of xib belongs to the FirstViewController and SecondView. the class of xib is SecondViewController, connects the buttons on the xib interface with IBAction, and connects the view under Outlets to the view on the interface. For example:

 







 

Next, MainWindow. xib is involved:
1. Do not use MainWindow. xib
(1) initialize the delegate window.

Self. window = [[[uiappswalloc] initWithFrame: [[UIScreenmainScreen] bounds] autorelease];

(2) Specify the rootViewController of the window where delegate is located, that is, SwitchViewController, which needs to be created and initialized.

Self. viewController = [[[SwitchViewControlleralloc] init] autorelease];

[Self. viewControllerinitView];

Self. window. rootViewController = self. viewController;

For example:




 

2. Create MainWindow. xib and use

Create a new window named MainWindow. xib. For example:

 



, Drag an Object from the Library to the interface, and specify its class as MVTestAppDelegate,




Drag a View Controller to the interface and specify the class as SwitchViewController.
Specify the class of File's Owner of MainWindow. xib as UIApplication, and connect its delegate with MVTestAppDelegate:




Next, select Test App Delegate, that is, MVTestAppDelegate. Connect viewController to Switch View Controller on the interface, and connect window to window,






After the connection is complete, you need to set it very important, that is, Info. plist. Because MainWindow. xib is created by yourself, you need to specify NSMainNibFile as MainWindow.




 

Run the command and the system will report the error "Applications are expected to have a root view controller at the end of application launch ", that is because the rootViewController view is not added to the delegate window when delegate is started. Although rootViewController (the Switch View Controller in MainWindow. xib) has been specified and initialized in MainWindow. xib, the view of this ViewController is not explicitly added. Note that the delegate window has been initialized in MainWindow. xib. Therefore, you do not need to pass

Self. window = [[[uiappswalloc] initWithFrame: [[UIScreenmainScreen] bounds] autorelease];
This parameter is no longer required. Otherwise, you will find that the view of rootViewController is blocked by the new white window. Click back to view the view of rootViewController in an instant when it exits the animation.
That is:
(1) Remove the delegate window initialization process:


// Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds] autorelease];

(2) Add the view of rootViewController to the delegate window:

// Although the rootViewController of the application has been defined and initialized in MainWindow. xib, it must be added to the delegate window.

[Self. viewControllerinitView];

[Self. windowaddSubview: self. viewController. view];

For example:




Note: The final running effect is displayed as the view of FirstViewController at startup. Click Show Second to switch to the view of SecondViewController. Click Show First to switch back, for example:




 

Summary:
If MainWindow. xib is not used, you need to use code to initialize the delegate window. At the same time, you also need to initialize the rootViewController of the window specified as delegate.
Use MainWindow. xib, Xcode4 and later versions must be created by yourself and added delegate and rootViewController. Also, pay attention to the connection between the delegate of File's Owner (class UIApplication) and the added delegate, and the window and viewController of the specified Delegate are connected to the window on the interface and the added viewController. You also need to specify the NSMainNibFile key value in info. plist. Finally, add the rootViewController view in MainWindow. xib to the delegate window in the delegate startup completion interface, and remove the default initialization process of the delegate window.

++ ++
(Supplement ):

(1) No MainWindow. in xib, you must specify the self. window and self. window. rootViewController, window, And rootViewController both need to define, allocate space, and initialize themselves. In the absence of ARC, you also need to control memory reclaim.

(2) There is MainWindow. xib, and in info. if NSMainNibFile is specified as MainWindow in plist, the default delegate window is MainWindow. the window in xib has been defined and initialized. We don't need to worry about memory reclaim or anything, and in MainWindow. if UIViewController is specified in xib, this UIViewController is also defined and initialized. We do not need to manage memory recycle like window. That is, we can directly use the window and UIViewController. The only note is: MainWindow. although we no longer need to define and initialize the UIViewController in xib, this UIViewController has not been added to self. window, so the only thing we need to do is to add the view of this UIViewController to self. window, or use this UIViewController to initialize a UINavigationController, and then add the view of this UINavigationController to self. window.

Note: MainWindow. in xib, you do not have to add or specify a View Controller. when xib is added for use, it does not have to be added to self as rootViewController. window. In other words, it can be added as the sub-view controller of rootViewController, only in MainWindow. you do not need to define or initialize it when you add it to xib (the premise is that you need to add the IBOutlet of the viewController in the application delegate header file, and in MainWindow. select the delegate on the xib interface, and associate the newly added IBOutlet in the delegate with the viewController on the interface ). If it is not in MainWindow. add the existing xViewController in xib. h. xViewController. m, xViewController. in the case of xib, use alloc init autorelease before use (if there is no retain in use, do not use autorelease) for definition and initialization and proper control of memory reclaim.

(Note: Later we found that self. window. rootViewController = self. myNavController; and [self. window addSubview: self. myNavController. view]; are equivalent .)

Through these, we can guess or in MainWindow. add multiple view controllers to xib, add the IBOutlet corresponding to the view controller in the application delegate, and add the IBOutlet corresponding to the view controller in MainWindow. in xib, select the app delegate to associate the added IBOutlet with the view controller on the interface, and then you can directly use these in MainWindow in the app delegate. the view controller added in xib does not need to be concerned with space allocation, initialization, and memory recovery management.

In addition, when you create a UIViewController, you can select With XIB for user interface. Then, the UIViewController Interface can be designed directly in the corresponding xib file through interface Builder, it can also be directly in the corresponding *. in m, code is designed in viewDidLoad. Specifically designed in xib and associated and used in code, it is the problem of adding IBOutlet and IBAction for drag-and-drop connection, in the previous section "iPhone Development Study Notes 001 -- How to associate controls with codes on the Xib interface.
++ ++

Some of the content in this article comes from the blog "walking space" on the Internet, which mainly defines and implements SwitchViewController! Make a record to avoid forgetting it for a long time! I have been involved in iPhone development for a short time and may have misdescriptions in many places. Please help me. Thank you very much!

 

From Code Heaven




 

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.