1

I have what I think is a simple problem. I have three views in my app, View1, View2, View3. I know how to go from View1 to View2, and from View2 to View3, but how do I go from View3 back to View1?

I am using the following method in the View1ViewController.m to go from View1 to View2:

[self presentModalViewController:view2ViewController animated:YES]

And the same thing to go from View2 to View3.

I am including the View2 view controller in the View1 XIB file, and am including the View3 view controller in the View2 XIB file.

I've found that if I use [self dismissModalViewControllerAnimated:YES] from View3 I can only go back to View2, whereas I need to go all the way back to View1.

Any help/direction would be greatly appreciated.

Many thanks, -Sd

SD.
  • 37
  • 6

3 Answers3

2

Don't think of calling -presentModalViewController: as a way of going from one view controller to another; think of it as a way of, well, presenting a view controller modally. If you want to implement navigation, you should take a look at UINavigationController.

jlehr
  • 15,557
  • 5
  • 43
  • 45
  • Is the UINavigationController the right fit? My View2 does absolutely nothing except show a picture for 1.5 seconds, before opening View3. – SD. Oct 13 '10 at 23:24
  • I guess that depends on what you mean by 'opening' the third view. But a modal view controller suggests a user interaction (usually involving capturing user input), so that's definitely the wrong paradigm. Instead, why not just have the first view controller add a subview or layer to display the animation? – jlehr Oct 13 '10 at 23:39
  • I like the sounds of that option - adding a subview to the View1 view controller that handles the animation. What methods can I use to handle that? – SD. Oct 13 '10 at 23:52
  • Depends how you're creating the second view; if you create it in IB, then all you'd need to do is send an `-addSubview:` message to the first view. When the animation completes, you could then either send `-removeFromSuperview` to the second view, or just set its `hidden` property to YES. – jlehr Oct 13 '10 at 23:57
  • Right now I have all three views in IB. So, in this case, I'm getting rid of my View2 completely, and sort of embedding it as a subview in View1. So, View2 becomes a subview of View1, and View3 becomes View2. – SD. Oct 13 '10 at 23:59
  • jlehr: Can I add a subview by calling its XIB? – SD. Oct 14 '10 at 00:04
  • Not sure I know what you mean by 'calling its XIB.' – jlehr Oct 14 '10 at 00:10
  • A nib (not xib; that's a compile-time artifact that doesn't exist at runtime), is a file in the filesystem -- that's not something that you can call. Did you mean load the nib? If so, that's not enough; you still have to send an `-addSubview:` message to the first view. – jlehr Oct 14 '10 at 00:15
  • Yes - sorry, that's what I meant. Thanks very much for all of your help. Trying all of this now... – SD. Oct 14 '10 at 00:27
1

From your description, it sounds like you're using modal views incorrectly. They are not a way to transition between views, they are a way to briefly show some dialog that will be dismissed soon after (like selecting a date or something). Are you sure that both your views are actually modal views, and not completely distinct?

Ian Henry
  • 22,255
  • 4
  • 50
  • 61
  • In my app, View2 is just a brief animation that shows a picture between View1 and View3. It doesn't do anything, really. But I need to be able to get from View3 back to View1. Can you suggest another method? – SD. Oct 13 '10 at 23:16
1

You might consider using a navigation controller to switch between views. Adding views with the pushViewController method. You could then use the (NSArray *)popToRootViewControllerAnimated:(BOOL)animated method in the UIViewController class. There is also popToViewController:animated: that will allow you to pop to a specific viewController.

If you are set on using modal views you can implement a protocol in view3 that view2 implements. When you are ready to pop to view1 from view3 you can call [self dismissModalViewControllerAnimated: YES]; then use your protocol to notify view2 that it should also dismiss its modal view.

Hope this helps.

jmurphy
  • 1,891
  • 3
  • 22
  • 28
  • Really appreciate the comment. In my case, View2 does absolutely nothing other than show a picture for 1.5 seconds, before opening up View3. Can you elaborate on implementing a protocol? – SD. Oct 13 '10 at 23:26
  • 1
    If I were you I'd go with a UINavigationController. When your animation finishes push view3 onto the stack. If you want to go the protocol route check out this link. https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProtocols.html . Scroll down until you see the heading Formal Protocols. It's pretty simple and a technique that will come in very handy down the road when you need to communicate between views. – jmurphy Oct 13 '10 at 23:30