views:

222

answers:

3

I've been reading the Head First iPhone Development book and I understand how to get to a new view from a table but how exactly would I be able to get to a new view or view controller, by just simply pressing a button? Is that even possible?

I mean there are some apps where you click a button, not a table cell and it loads a new view. How exactly is that done? If someone could help out a newbie it would be greatly appreciated!

A: 

I think what you're looking for is a modal vew controller. THis presents a modal view like you described on top of everything else. If rootViewController is the view controller that is displaying your current view, and myNewViewController the view controller you want to display modally:

[rootViewController presentModalViewController:myNewViewController animated:YES];

There's plenty of examples of this kind of thing on the net, just search for presentModalViewController

pheelicks
A: 

Like bpapa said in the comments, it's hard to be specific without code. However, generally what you want to do is:

  • Build a navigation controller that contains one original view.
  • Create a button in your original view using the Interface Builder.
  • Build a callback method (usually defined with IBAction) that is run when the button is pushed.
  • In that callback method, create a new view and push it onto the navigation controller the same way you would using a table view cell.

Alternately, if you only want one level of hierarchy, you could use a modal view controller; instead of pushing onto the navigation controller in the last step, just present the modal view controller.

Tim
A: 

The general answer is that you have an object that manages which view controller loads when.

The most commonly used is the UINavigationController. It is a UIViewController that instead of controlling views, controls other view controllers. It works like a simple stack. You push views you want to display onto the nav's controller stack and when you want them to disappear you pop them off.

A common (though sloppy) way of using a nav is to make it a property of your app delegate. Then anywhere in your app you can references it by:

UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];

The view controller for the first the user sees is held in the nav's topViewController property. If you want to load a view based on a user action in the topViewController.view, you would have something like this:

- (IBAction) loadNextView:(id) sender{ // Action called by a a UI event such as a button press. 
    UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
    UIViewController *nextViewController=...// load from nib, connect with IBOutlet, create programmatically
    [nav pushViewController:nextView animated:YES];
}

The first view disappears to be replaced by the next one. To return to the first view, you have a method in the next view controller like so:

- (IBAction) unloadSelf:(id) sender{ // Action called by a a UI event such as a button press. 
    UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];

    [nav popViewControllerAnimated:YES];
}

... and the nav returns you automatically to the previous view regardless of what that view was.

When you first start out, especially if you use Interface Builder, the structure of the app is largely hidden. Behind the scenes all view controllers and their views exist in a hierarchy of some kind that leads back up to the app delegate. You should train yourself to think in hierarchal terms even if it is not immediately obvious how that hierarchy is constructed.

TechZen