Learn how to create and switch a view
Mac OS X 10.7.2
Xcode4.2
- Details
- Create another viewcontroller In the storyboard and use segue to switch
Add another viewcontroller to the storyboard. Add a button to each of the two viewcontrollers. Right-click the button and drag and drop the "Modal" circle in the pop-up menu to another viewcontroller.
- Create another viewcontroller in the xib file and use the code to manually switch
Add a file to the project, select create "uiviewcontroller subclass", select "with XIB for user interface" in the wizard, and name it "secondviewcontroller". Then, 3 files are displayed: "secondviewcontroller. H "," secondviewcontroller. m "," secondviewcontroller. XIB ".
Add a button in XIB and add an event handler for it. Add the following code to the function to exit the current view and return to the home page:
- (IBAction)exitCurrentView:(id)sender { [self.view removeFromSuperview];}
Add the corresponding XIB variable to viewcontroller. H on the homepage, as shown below:
@interface ViewController : UIViewController{ SecondViewController* secondViewController;}
The code of the switch button event function is as follows:
- (IBAction)switchToSecondView:(id)sender { secondViewController=[[SecondViewController new] initWithNibName:@"SecondViewController" bundle:nil]; [self.view addSubview:secondViewController.view];}
- Manually create a view in the Code and use the code to manually switch
-(Ibaction) switchtothirdview :( ID) sender {// create view thirdview = [[uiview alloc] initwithframe: Self. view. bounds]; thirdview. backgroundcolor = [uicolor greencolor]; // Add the control uibutton * button for the view = [uibutton buttonwithtype: uibuttontyperoundedrect]; button. frame = cgrectmake (100,100,100,100); [Button settitle: @ "" forstate: uicontrolstatenormal]; [Button addtarget: Self action: @ selector (exitthirdview :) forcontrolevents: uicontroleventtouchupinside]; [thirdview addsubview: button]; // display the view. // Add it to the animation bar. [uiview beginanimations: @ "flipping View" context: Nil]; [uiview setanimationduration: 1]; [uiview setanimationtransition: uiviewanimationtransitionflipfromleft forview: Self. view cache: No]; [self. view addsubview: thirdview]; [uiview commitanimations];}-(void) exitthirdview :( ID) sender {// Add the animation effect to it [uiview beginanimations: @ "flipping View" context: nil]; [uiview setanimationduration: 1]; [uiview setanimationtransition: uiviewanimationtransitionflipfromright forview: Self. view cache: No]; [thirdview removefromsuperview]; [uiview commitanimations];}
- Watch this development video online