Manual view switching for iPhone

Source: Internet
Author: User

Manual view switching for iPhone



We used uinavigationcontroller to switch the view. Today we will introduce how to switch the view manually:


The switching principle is simple:

There is a root uiviewcontroller class that contains the viewcontroller corresponding to the views to be switched. When switching, the current view is deleted first, and then the insertsubview is called to add the view after switching to complete the switching.




1.

First, create a view-based application named switch:





2.

For example, create two new uiviewcontroller subclass, firstviewcontroller and secondviewcontroller:






3.

Add a switch button in switchviewcontroller. XIB:




Modify firstviewcontroller. XIB and secondviewcontroller. XIB to distinguish them, as shown in:

Firstviewcontroller. XIB:

Secondviewcontroller. XIB:


You can call the following in viewdidload of viewcontroller:

self.view.backgroundColor = [UIColor yellowColor];

To change the background color of the view to yellow.





4.

Modify switchviewcontroller. h as follows:

////  SwitchViewController.h//  Switch////  Created by HuTao on 8/18/12.//  Copyright __MyCompanyName__ 2012. All rights reserved.//#import <UIKit/UIKit.h>@class FirstViewController;@class SecondViewController;@interface SwitchViewController : UIViewController{FirstViewController * firstViewController;SecondViewController * secondViewController;}@property (retain, nonatomic) FirstViewController * firstViewController;@property (retain, nonatomic) SecondViewController * secondViewController;-(IBAction)btnSwitchView:(id)sender;@end

Modify switchviewcontroller. m as follows:

//// Switchviewcontroller. M // switch /// created by FIG on 8/18/12. // copyright _ mycompanyname _ 2012. all rights reserved. // # import "switchviewcontroller. H "# import" firstviewcontroller. H "# import" secondviewcontroller. H "@ implementation switchviewcontroller @ synthesize firstviewcontroller; @ synthesize secondviewcontroller;-(ibaction) btnswitchview :( ID) sender {If (self. secondviewcontroller = nil) {// secondviewcontroller * temp = [[secondviewcontroller alloc] init] or [secondviewcontroller * temp = [[secondviewcontroller alloc] initwithnibname: @ "secondviewcontroller" Bundle: nil]; self. secondviewcontroller = temp; [temp release];} [uiview beginanimations: @ "view flip" context: Nil]; [uiview setanimationduration: 1.25]; [uiview setanimationcurve: timeout]; uiviewcontroller * coming = nil; uiviewcontroller * Going = nil; uiviewanimationtransition transition; If (self. firstviewcontroller. view. superview = nil) {coming = firstviewcontroller; going = secondviewcontroller; // from right to left transition = uiviewanimationtransitionflipfromright;} else {coming = secondviewcontroller; going = firstviewcontroller; // left-to-right transition = uiviewanimationtransitionflipfromleft;} [uiview setanimationtransition: Transition forview: Self. view cache: Yes]; [coming viewwillappear: Yes]; [going viewwilldisappear: Yes]; [going. view removefromsuperview]; [self. view insertsubview: Coming. view atindex: 0]; [coming viewdidappear: Yes]; [going viewdiddisappear: Yes]; // submit animation [uiview commitanimations];} // implement viewdidload to do additional setup after loading the view, typically from a nib. -(void) viewdidload {[Super viewdidload]; firstviewcontroller * temp = [[firstviewcontroller alloc] initwithnibname: @ "firstviewcontroller" Bundle: Nil]; self. firstviewcontroller = temp; [self. view insertsubview: temp. view atindex: 0]; [temp release];}-(void) viewdidunload {firstviewcontroller = nil; secondviewcontroller = nil;}-(void) dealloc {[Super dealloc]; [firstviewcontroller release]; [secondviewcontroller release];} @ end

The above shows that two views are switched to each other, but more views are switched in the same principle.





5.

Connect the switch and touch up inside callback functions in interface builder.




6.

The running result is as follows:

Before switchover:



Switching:



After switching:






The animation section will be in:

Http://blog.csdn.net/htttw/article/details/7879535

.




Finally, I uploaded the complete code:

Http://download.csdn.net/detail/htttw/4508503





Done!

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.