008 create an ios6.0 application on xcode4.5 (multi-view application)

Source: Internet
Author: User
The Root View application (switching between two views) is important to switch multiple views on iOS, and it is also a key point to understand. The programs developed in IOS basically all have multiple views. Next I will take you to create an application with multiple views. First, let's take a look at the actual running

Click the switch View button below to switch the view.

The directory outline creates two different views. Step 1: select group, click right, select new file, Step 2: Select cocoatouch under iOS, and then object_c. Click Next Step 3: click "CREATE" and click "Next" to complete the class. There are many options for writing your own class Ming subclass. here we need to select the uiviewcontroller object. Note: Remember to hook the with XIB for user interface option.
In the following example, you only need to design the interface and then improve the view. You only need to follow the code below and then execute the code to see the above effect viewcontroller. h. Remember to introduce the hware of two subview controls in the main view's H file.

# Import "yellowviewcontroller. H"

# Import "blueviewcontroller. H"

@ Interface viewcontroller: uiviewcontroller {yellowviewcontroller * yellowviewcontroller; blueviewcontroller * blueviewcontroller;} @ property (nonatomic, retain) yellowviewcontroller * yellowviewcontroller; @ property (nonatomic, retain) blueviewcontroller * blueviewcontroller; // define the button to switch to the view (ibaction) switchviews :( ID) sender; @ end

Viewcontroller. m

@ Implementation viewcontroller @ synthesize yellowviewcontroller; @ synthesize blueviewcontroller; // define a button to switch between views-(ibaction) switchviews :( ID) sender {// Replace the view with each other // the first judgment is to determine whether the yellow view has the IF (self. yellowviewcontroller. view. superview = nil) {// determine whether the current reality is a yellow view if (self. yellowviewcontroller. view = nil) {yellowviewcontroller * yellowviewcv = [[yellowviewcontroller alloc] initwithnibname: @ "yellowviewcontroller" Bundle: Nil]; self. yellowviewcontroller = yellowviewcv; [yellowviewcv release];} // remove the Blue View [self. blueviewcontroller. view removefromsuperview]; [self. view insertsubview: yellowviewcontroller. view atindex: 0];} else {If (self. blueviewcontroller. view = nil) {blueviewcontroller * blueviewcv = [[blueviewcontroller alloc] initwithnibname: @ "blueviewcontroller" Bundle: Nil]; self. blueviewcontroller = blueviewcv; [BL Ueviewcv release];} [self. yellowviewcontroller. view removefromsuperview]; [self. view insertsubview: blueviewcontroller. view atindex: 0] ;}}// call the Blue View-(void) viewdidload {[Super viewdidload] When the program is loaded. blueviewcontroller * blueviewcv = [[blueviewcontroller alloc] initwithnibname: @ "blueviewcontroller" Bundle: Nil]; self. blueviewcontroller = blueviewcv; // Why is there a release after the number member variable is created here? The reason is that you want to release resources, because in the H file, this member variable is defined as the retain type in @ property (nonatomic, retain). This type can retain the member variable [blueviewcv release]; // Insert the view to the main view. Note that the following parameter 0 is used to place the view to the most advanced layer [self. view insertsubview: blueviewcontroller. view atindex: 0];}-(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} -(void) dealloc {[yellowviewcontroller release]; [blueviewcontroller release]; [Super dealloc];} @ end

Yellowviewcontroller. h
@interface YellowViewController : UIViewController-(IBAction)yellclick:(id)sender;@end

Yellowviewcontroller. m

@ Implementation yellowviewcontroller-(ibaction) yellclick :( ID) sender {uialertview * Alert = [[uialertview alloc] initwithtitle: @ "Emergency prompt" message: @ "fake hands, touch, do not want to live. "delegate: Nil cancelbuttontitle: @" "otherbuttontitles: @" I want to touch ", nil]; [alert show]; [alert release];}-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: role]; If (Self) {// custom initialization} return self ;} -(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view from Its nib .} -(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
Blueviewcontroller. h
@interface BlueViewController : UIViewController-(IBAction)blueclick:(id)sender;@end

Blueviewcontroller. m

@ Implementation blueviewcontroller-(ibaction) blueclick :( ID) sender {uialertview * Alert = [[uialertview alloc] initwithtitle: @ "Emergency prompt" message: @ "dear, I miss you. "delegate: Nil cancelbuttontitle: @" miss you "otherbuttontitles: @" I miss you too ", nil]; [alert show]; [alert release];}-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: role]; If (Self) {// custom initialization} return self ;} -(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view from Its nib .} -(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end
In this way, our multi-view application is complete.

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.