IOS development (3) view creation and switching

Source: Internet
Author: User
  • Purpose

Learn how to create and switch a view

  • Environment

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];}

  • Preview

  • Watch this development video online

  • Source code download
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.