Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to properly switch view controllers?

RomanRobotRomanRobot Posts: 46Registered Users
edited July 2011 in iOS SDK Development
So I've been looking everywhere for a clear answer and can't seem to find one. I tried Google, Documentation, YouTube and searching this forum. I try not to annoy anyone, especially with these very basic questions, but I'd like to know the proper way to switch between view controllers. Just how to set it up and tell it to switch. Thanks for your help! ^.^
Post edited by RomanRobot on

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2011
    Navigation controller is most common, but it depends on what you want to do.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • RomanRobotRomanRobot Posts: 46Registered Users
    edited July 2011
    BrianSlick wrote: »
    Navigation controller is most common, but it depends on what you want to do.

    Thanks for the response but it's for a game so the navigation controller isn't ideal. I just want to switch between custom view controllers which each have their headers, implementations, and nibs.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2011
    Navigation controllers do not have to display navigation bars.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • xcoderxxcoderx Posts: 42Registered Users
    edited July 2011
    RomanRobot wrote: »
    Thanks for the response but it's for a game so the navigation controller isn't ideal. I just want to switch between custom view controllers which each have their headers, implementations, and nibs.

    easy just use:

    MyViewController * controller = [[MyViewController alloc]init];
    controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:controller animated:YES];
    controller.release;
  • RomanRobotRomanRobot Posts: 46Registered Users
    edited July 2011
    xcoderx wrote: »
    easy just use:

    MyViewController * controller = [[MyViewController alloc]init];
    controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:controller animated:YES];
    controller.release;

    Well idk, I don't want to use the presentModalViewController because when I switch between view controllers I want to completely and utterly release everything from the last view so if I were to go back to the last view it would do the viewDidLoad and everything. So for what I want would the navigation controller work best?
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2011
    You can manipulate the navigation stack to get rid of the previous controller. Or it will take care of unloading itself if memory gets low.

    Start with the navigation controller. Prove that you have a problem before you try to solve it.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • RomanRobotRomanRobot Posts: 46Registered Users
    edited July 2011
    I got the navigation controller thing up and running, thanks for the info. There's only one thing I'd like to know how to do. Let's say I push a landscape view controller, how could I make the whole thing landscape while that view controller is being displayed?
  • RomanRobotRomanRobot Posts: 46Registered Users
    edited July 2011
    So I solved my own problem again, probably the wrong way again, but I just told the navigation to rotate to the landscape orientation but on it's willRotateToInterfaceOrientation:duration: I pushed the view controller. This way when the next one is loaded it will be asked to turn to landscape, which it will. Works beautifully. :)
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2011
    You might want to show how you're doing that.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • RomanRobotRomanRobot Posts: 46Registered Users
    edited July 2011
    BrianSlick wrote: »
    You might want to show how you're doing that.
    Haha thanks for caring whether I'm doing things right. :P

    So what I want is so that when the root view controller of the navigation controller is rotated to landscape, to push the next view controller (SkirmishMenuViewController) and display it in landscape.

    At the bottom of the RootViewController:
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            LoLAppDelegate *appDelegate = (LoLAppDelegate *)[[UIApplication sharedApplication]delegate];
            SkirmishMenuViewController *skirmishMenuViewController = [[SkirmishMenuViewController alloc] init];
            [appDelegate.navigationController pushViewController:skirmishMenuViewController animated:NO];
            [skirmishMenuViewController release];
        }
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            return YES;
        }
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    

    If I were to just push it from the shouldAutorotateToInterfaceOrientation, the SkirmishMenuViewController would just show up in portrait. This way it shows up in landscape.

    Unrelated yet related question: When I pop a view controller it goes to the view controller under it right? Well instead of continuing where I left off (because it was still there, under the whole time) how can I start it from scratch again? I don't want this for all the view controllers just some.
    Unrelated but related question 2: How do I remove a specific view controller or view controllers from the stack so that I can for example remove view controller 3 and when view controller 4 (the top view controller) is popped it goes to view controller 2.
  • new2objectivecnew2objectivec Posts: 44Registered Users
    edited July 2011
    Just my 2c, I have a test multiple view switching project here on my blog which is based on Nick's space bubble open source game. I also based my simple TicTacToe game on that too, may be you can have a look?
    <a href="http://new2objectivec.blogspot.com/2011/10/4th-open-source-game-follow-me-if-you.html"; target="_blank">My 4th Open Source Game: Follow Me If You Can!!!<img src="http://www.iphonedevsdk.com/forum/images/smilies/smile.gif"; border="0" alt="" title="
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2011
    RomanRobot wrote: »
    At the bottom of the RootViewController:

    Have you tested this very much? Because it looks to me like any of the following could be true:

    1. If you rotate your device to landscape, you will automatically go to the next view.
    2. If the view controller this code is in is the, say, 2nd view controller, then if you were in landscape in the 1st view controller, you would skip the 2nd one and go straight to the 3rd one.

    You are enabling rotation, but you aren't actually forcing rotation. Also, you don't need to go to the app delegate for the navigation controller. You can use the view controller's navigationController property.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • RomanRobotRomanRobot Posts: 46Registered Users
    edited July 2011
    1. Well yeah, I want it to go the the next view when I rotate the device.
    2. I don't get how it would push a third view controller not even mentioned in the code. \"

    I don't know how to force a rotation, that would be very handy. The other two questions I had would also be very handy to know.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2011
    My point is that your solution doesn't seem to match your stated goal.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • RomanRobotRomanRobot Posts: 46Registered Users
    edited July 2011
    Yeah you may be confused because at the beginning it was to get a sample project working and now it's for another project and I want different things to happen in each one. It works for what I want though. If you know how to force a rotation like [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight] (which doesn't work anymore) that would prevent this work around. I'd also like to know how to start a view controller from scratch. Could I just pop it and push it really quick or would that cause problems?
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2011
    You can pop without animation, or you can change the view controller stack. See the docs.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
Sign In or Register to comment.