luisjd
luisjd

Reputation: 43

Change views in a UIViewController

I have an UIViewController with a UIToolBar at the top with 3 buttons and a UIView, when touch upInside those buttons I have to change the views that the controller has. What can I do to get my porpuse? Thanks in advance.

Upvotes: 0

Views: 3536

Answers (3)

J S Rodrigues
J S Rodrigues

Reputation: 471

UiViewController view property is the base view you are seeing. It could be set(replaced with another). SO replace the view object of ViewController with another view you created.

UIView * customView = [[[UIView alloc] initWIthFrame:viewFrame] autorelease];
[self setView:customView];

Here self represent the current viewController.

Upvotes: 2

user843337
user843337

Reputation:

You need to do something like this for each of the actions you set up.

In the .h file of the current viewController:

#import "OtherViewController.h"

@interface MyViewController : UIViewController
{
    OtherViewController *otherViewController;
}

@property(nonatomic, retain)IBOutlet OtherViewController *otherViewController;

Then in the .m file of the current viewController you need to add the following for each IBAction (touch up inside).

At the top of the .m file add:

@synthesize otherViewController;

Then make an IBAction and put the following line of code to display the other view:

[self presentModalViewController:otherViewController animated:NO];

In your otherViewController you can dismiss itself by using:

[self dismissModalViewControllerAnimated:NO];

NOTE: The other thing you will need to do is create a UIViewController in Interface Builder for each of the views you plan to display. You need to then go into the identity inspector and set the Class as OtherViewController. You then need to link the IBOutlet to the OtherViewController as normal.

There is a YouTube video tutorial which covers all of what I have mentioned above. It's a nice simple way to get started.

Upvotes: 2

chown
chown

Reputation: 52778

You probably want to use something like a UINavigationController to control the view stack and then have your button(s) call one of these methods for the Touch Up Inside action:

  • pushViewController:animated:
  • popViewControllerAnimated:
  • popToRootViewControllerAnimated:
  • popToViewController:animated:

Here is a good uinavigationcontroller-tutorial to look into.

Upvotes: 2

Related Questions