Problems switching views iOS & xcode

Discussion in 'iOS Programming' started by Lonewolff, Aug 26, 2012.

  1. Lonewolff macrumors newbie

    Aug 26, 2012
    Hi guys,

    I am having troubles with transitioning between two views.
    I have created a button to call switchViews. But, when I press the button the view turns white.

    The view(s) creation is as follows;

        // Create main window and root view controller
        self.mainWindow=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        self.mainWindow.backgroundColor=[UIColor whiteColor];
        self.rootViewController=[[ViewController alloc] init];
        [self.mainWindow makeKeyAndVisible];
        // Create child view controllers
        self.firstViewController=[[ViewController alloc] init];
        [self.rootViewController addChildViewController:self.firstViewController];
        self.secondViewController=[[ViewController alloc] init];
        [self.rootViewController addChildViewController:self.secondViewController];
        // Get screen boundaries
        CGRect screenBound = [[UIScreen mainScreen] bounds];
        CGSize screenSize = screenBound.size;
        CGRect bounds=self.mainWindow.bounds;
        // Create view1 and assign to firstViewController
        view1=[[UIView alloc]initWithFrame: bounds];
        [view1 setBackgroundColor:[UIColor yellowColor]];
        [self.mainWindow addSubview:view1];
        // Create view2 and assign to secondViewController
        view2=[[UIView alloc]initWithFrame: bounds];//CGRectMake(100,100,100,100)];
        [view2 setBackgroundColor:[UIColor redColor]];
        // Button to switch from view1 to view2
        UIButton* OurButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [OurButton setTitle:@"Switch to 'View 2'" forState:UIControlStateNormal];
        [OurButton setTitleShadowColor:[UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f] forState:UIControlStateNormal];
        [OurButton setTitleColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f] forState:UIControlStateNormal];
        [OurButton setFrame:CGRectMake(22,screenSize.height-(44*2),screenSize.width-44,44)];       
        [view1 addSubview:OurButton];
        [OurButton addTarget:self action:@selector(switchViews) forControlEvents:UIControlEventTouchUpInside];
    And to switch the views I have the following;

    - (void)switchViews
        [UIView beginAnimations:@"View Flip" context:nil];
        [UIView setAnimationDuration:1.25];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        UIViewController *coming=nil;
        UIViewController *going=nil;
        UIViewAnimationTransition transition;
            transition = UIViewAnimationTransitionFlipFromRight;
        [UIView setAnimationTransition: transition forView:view1 cache:YES];
        [coming viewWillAppear:YES];
        [going viewWillDisappear:YES];
        [going.view removeFromSuperview];
        [view1 insertSubview: coming.view atIndex:0];
        [going viewDidDisappear:YES];
        [coming viewDidAppear:YES];
        [UIView commitAnimations];    
    Any help would be awesome as I am an iOS / XCode / Mac newbie, with solid programming experience under Windows & C++.

    Trying to come to the light side!
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Why use code at all? Is there any particular reason you're not using storyboard and segues? Ray Wenderlich has a very nice tutorial on storyboards - I suggest looking at it here:

    If you want a standard looking/behaving iOS app, use storyboards. If you want it heavily customized... IDK what to do... I do my games mostly in code, but I don't know if other people have a better way of using Xcode's built in graphical design tools.
  3. dejo, Aug 27, 2012
    Last edited: Aug 27, 2012

    dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Lonewolff, a few things:

    - Where does the code from your first snippet reside? It means less when it lacks the context of where it is run.

    - Assuming it is in your appDelegate's application:didFinishLaunchingWithOptions:, it is not usual to put the button handler code in your appDelegate as well. switchViews should be a part your viewController's code.

    - OurButton does not follow standard naming conventions. It looks like a class name, when in fact it represents a class instance. ourButton would follow the standard, in this case.

    - Have you looked at using UIViewController's presentViewController:animated:completion: method? It would reduce the amount of code in your switchViews method.

    - What resources are you using to educate yourself about the fundamentals of Objective-C / iOS development?


    P.S. How and where are view1 and view2 defined?

Share This Page