How to Switch between multiple views

Discussion in 'iOS Programming' started by fzaman, Jul 10, 2011.

  1. fzaman macrumors newbie

    Joined:
    Jun 16, 2011
    #1
    I'm a newbie and trying to build a multiview app and can't really get my head straight on what approach is the best. I'm interested to know how to switch from one controller to another without clicking a Tabbar, Navigationbar or a Button. Basically there'll be a puzzle on first view controller's view which when 'solved' will switch to second view controller and so on.
    Any help is appreciated.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    The code you would run inside your IBAction for a button click is the same code you can run based on any other trigger, such as when the first puzzle is solved.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    In many cases, if you don't want the appearance of a navbar you can still use a navigation controller, but use a hidden navbar. Then you can use the navigation controller to push/pop view controllers.
     
  4. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    There are so many options if u wouldve taken a look around.

    A nav app without navbar, so you have the options laying around for you, for switching, and popping etc. You could do a Utility app, that does 1 thing, and flips around the view to the back.
    You could also do a normal window app, just add a modalviewcontroller with "Solved" on it.

    Alot of things to try out imo.

    Gl.
     
  5. fzaman thread starter macrumors newbie

    Joined:
    Jun 16, 2011
    #5

    Dejo,
    I followed an example of multiple views from here

    http://stackoverflow.com/questions/...-how-to-switch-between-views-programmatically

    And changed the - (IBAction)switchViews method with (void) method and kept the code same. Then on blue view controller's view class I created an instance of SwitchViewController and call out switchViews method when the puzzle is solved but somehow the blue view controllers view doesn't get removed. Am I doing something wrong here ?
     
  6. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    I think it is best to check out a iPhone book which has everything you are asking for right in there, with source, or by following the book. That will improve your skills way better then this ping ponging of answers.. What did you try to delete the view, how did you do it, what doesn't it do. How did you check if it didn't work.
    We don't know what u did/tried, so can't give decent answers. Don't really know how to put it else.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    It's really hard to know if you're doing something wrong because it is rather difficult to debug a description of your code. Post your code and we can better assess what the issue might be.
     
  8. fzaman thread starter macrumors newbie

    Joined:
    Jun 16, 2011
    #8
    dejo,
    Is it possible to send you the code via email ? its impossible to post entire everything over here. Please shoot me an email at fzaman2@gmail.com and i'll send you the code. Thank you very much for cooperating.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    I think it would be better to post the code here and have multiple eyes looking at it vs. emailing it to me and only having me look at it, when I get a chance.

    And you shouldn't need to post your entire code here. What you are asking for should be simple enough that a few methods (and perhaps some accompanying explanation and/or .h file) should suffice.

    I would suggest posting your switchViews method as well as the code for the method that calls it.
     
  10. fzaman, Jul 13, 2011
    Last edited by a moderator: Jul 14, 2011

    fzaman thread starter macrumors newbie

    Joined:
    Jun 16, 2011
    #10
    Here is the viewDidLoad and switchView method of root view controller class "Switch View Controller"


    Code:
    - (void)viewDidLoad
    {
    
        BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];
        self.blueViewController = blueController;
        [blueController release];
        [blueViewController viewWillAppear:YES];
        [self.view addSubview:blueViewController.view];
        [blueViewController viewDidAppear:YES];
        [super viewDidLoad];
        
    }
    
    -(void) switchView
    {
       
        if (self.yellowViewController.view.superview == nil)
        {
            if (self.yellowViewController == nil)
            {
                
                YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@"YellowViewController" bundle:nil];
                self.yellowViewController =  yellowController;
                [yellowController release];
            }
            [blueViewController viewWillDisappear:YES];
            [blueViewController.view removeFromSuperview];
            
            [yellowViewController viewWillAppear:YES];
            [self.view addSubview:yellowViewController.view];
            [blueViewController viewDidDisappear:YES];
            [yellowViewController viewDidAppear:YES];
        }
        else if (self.blueViewController.view.superview == nil)
        {
            if (self.blueViewController == nil)
            {
                NSLog(@"self.blueviewcontroller = nil");
                BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];
                self.blueViewController = blueController;
                [blueController release];
            }
            [yellowViewController viewWillDisappear:YES];
            [yellowViewController.view removeFromSuperview];
            [blueViewController viewWillAppear:YES];
            [self.view addSubview:blueViewController.view];
            [yellowViewController viewDidDisappear:YES];
            [blueViewController viewDidAppear:YES];
        }
        
    }
    And touchesMoved method inside "FirstView" class of "Blue View Controller" which creates instance of switch view controller and calls out switchViews

    Code:
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *t = [touches anyObject];
        CGPoint touchLocation = [t locationInView:self];
        CGRect squareRect = [square frame];
        
        if (CGRectContainsPoint(squareRect, touchLocation))
        {
            square.center = touchLocation;
            if (CGRectContainsPoint([destination frame], touchLocation))
            {
                square.center = destination.center;
                SwitchViewController *switchController = [[SwitchViewController alloc] initWithNibName:@"SwitchViewController" bundle:nil];
                self.switchViewController = switchController;
                [switchController release];
                [self.switchViewController switchView];
                
            }
        }
        
    }
    Let me know what you think of it.
    Thanks
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    This sounds confusing to me. I get that touchesMoved is a method, but where does it reside? Do you have a FirstView class? Or does this method reside within BlueViewController.m? Perhaps elaborate on what you are trying to say with that statement.
     

Share This Page