Return to Tab Bar View From Standard View

Discussion in 'iOS Programming' started by newtoiphonesdk, Nov 10, 2010.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I have a tab bar app that I have built. On one of the tabs, there is a quiz I wrote. It is very basic, there is a question, and 4 round rect buttons, they are coded like this using IBActions
    Code:
    [self presentModalViewController:tenthView animated:YES];
    with the wrong answers going to a view that says incorrect and the correct one to a view with the congratulations. My question is, when the quiz is over, how can I code an IBAction to go back to the Tab Bar? All of my questions are just standard views, and I am at a loss as how to do this. Thanks.
     
  2. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #2
    In the viewController implementation for the questions, just call this:

    Code:
    [self dismissModalViewControllerAnimated:YES];
    I'm assuming you have some sort of button that you'd like to have pressed that would dismiss the view. Typically it's like a "Done" button. Just have write up the IBAction like it seems you already know how to do, and call that.
     
  3. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    I would do that but there are several questions and using that line of code only takes me back to the previous screen. I need a way to get all the way from the last question back to the tab bar itself.
     
  4. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #4
    Oh, I see your problem. You're not quite organizing this right.

    What you should be doing is when you present your first question create a NavigationController initialized with the view for your first question. Then you call presentModalViewController for the NavigationController, NOT the first view.

    Then for each subsequent question modify your button to "Push" a new view controller onto the navigation controller, instead of presenting a new modal view controller. For example:

    Code:
    [self.navigationController pushViewController:secondQuestion animated:YES];
    That will slide your current view to the left, and your new one in from the right. It's a very standard way of handling navigation on the iPhone.

    Then, for your last question, when you dismiss the modal view controller the navigationController (as the root view controller for the modal view) goes away, and you're back where you started at the tab bar.

    Make sense?
     
  5. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #5
    Yes it makes sense. However, I've already laid this all out with every question as it is now. Is there any type of workaround I can use for this one instance that will take me back to the tab bar controller?
     
  6. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #6
    I guess you could call dismissModalViewController 10 times, heh. That's really the only alternative I could think of, and I wouldn't consider it a good practice.

    It really should be easy, like completely trivial to change it to what I've suggested.

    In your tab bar view controller where you present the first question, add this:

    Code:
    NavigationController *navigationController = [[NavigationController alloc] initWithRootViewController:firstQuestion];
    [self presentModalViewController:navigationController animated:YES];
    Then replace the button IBAction selector method for questions 1-9 to this:


    Code:
    [self.navigationController pushViewController:secondQuestion animated:YES];
    
    And leave the last button method as dismissModalViewController.

    It really is just that easy.
     
  7. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #7
    Ok, thanks. I'm at work with just my iPad now so it may be a bit before I can change anything but I do appreciate the advice. Is it still possible for me to just have one view for every wrong answer and set each wrong selector to the same view in this method?
     
  8. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #8
    Hrm, maybe you need to explain more about the exact structure of your app before we'd be able to answer that last question. The more detail the better. Are you presenting a different modal view based on each button selection for each question? I'm not quite sure what you mean there.
     
  9. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #9
    I have five questions total each with four choices. If you answer wrong it pulls up one view I have made with a button to return to the question to try again. If you answer correctly it pulls a view that says correct and gives more details about the question that was asked and has a button to proceed to the next question. Basically I dont need different views for every possible wrong answer and would like to reuse that same view for every wrong answer.
     
  10. cnstoll, Nov 10, 2010
    Last edited: Nov 10, 2010

    cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #10
    Ah, ok. Yeah that will all still work, but it depends on your method of doing those things for what you will need to change.

    For example, if you answer a question wrong, and present a new view that says "blah blah this was wrong...try again?" then presenting that view is exactly how I described (push the view controller on to the navigation controller, and you're done).

    The question there though is how are you going back to the question? Are you actually dismissing the "wrong answer" view (dismissModalViewController) or are you re-presenting the view for that question? If you're dimissing, then you would call the cooresponding navigationController method "popViewController" wh ich would return you to the question. You have another option with NavigationControllers though which is really quite nice. When you push a ViewController onto a NavigationController you get a built in back button on the top left corner. You could literally just use that as your back button. If you didn't want that back button on the navigationBar to appear, there are various ways of removing it or hiding it.

    Another option that you could do is treat each question as a "page" on your navigation controller. Think of your navigation controller as your quiz, which has 5 pages. Then for each page (question) you can present a modal view controller for the right/wrong answer. Then those two would do two things based on right/wrong answers. A wrong answer would simply dismiss the modal view controller, which would take you back to the question automatically. The right answer "next question" button would dismiss the modal view controller AND push the next question view controller (that would most likely involve implementing a delegate method though, which would let your right answer view controller tell the question view controller to move to the next view). This method of doing things could be better from a design standpoint, but the first method would definitely be easier.


    To be honest a lot of this is important stuff to know for iOS development. Using the right View Controller in the right way is very important for common human interface design for the iPhone. Look at some of Apple's apps for guidance. You'll find that modal views are used sparingly, and not as the core method of presenting information. The tool designed for that purpose IS the NavigationController. I highly recommend reading more on that and using what you learn to help with tweaking your application. It sounds like you've made a lot of progress with your app, but I think considering these common design practices would go along way towards polishing it up.
     
  11. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #11
    I'll probably use a navigation controller and simply dismiss the wrong answer. Then when the right answer is done Incan have a button merely push the next question onto the navigation controller? How would I setup the navigation controller at the beginning? I use ib for most of my stuff so that's what im familiar with.
     
  12. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #12
    To be honest I don't know how to set up a navigation controller in IB because everywhere that I've read about it has said it's easier to just do it in code. And honestly it really is. The code I wrote in post #5 or 6 is literally all you need to do to set up the Navigation Controller. There's other things you could do to customize things like the navigation bar color, and the buttons on the navigation bar, but that code segment should be all you need to make it work.
     
  13. newtoiphonesdk, Nov 10, 2010
    Last edited: Nov 10, 2010

    newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #13
    Ok, I got it all setup with a navigation controller. It flows in one nice form. All wrong choices slide in the view that is set that says incorrect. You then click the normal back button that comes with the navigation controller. When I get to the end I tried the dismissModalViewController to send it back to the very beginning, but it didn't work. How do I get back to the first one? Also, how do I create the title on the navigation bar for each new view that is pushed?

    UPDATE
    I used the popToRootView method to go back to the beginning.
     

Share This Page