Curnelious
Curnelious

Reputation: 1

Switching between viewControllers manually

I am in viewcontroller1 and i want to go to viewcontroller2 , in 1 i do :

UIViewController *sv=[[signUpView alloc]init];
[self addChildViewController:sv];

Where signUpView is the new viewController class(subclass of view controller) Nothing is happens .

I tried : [self.view removeFromSuperview]; which did worked and removed my view.

  1. how do i do transition to 2
  2. how do i add 2 on 1 and than go back to 1 ?

Thanks .

Upvotes: 0

Views: 78

Answers (3)

DBD
DBD

Reputation: 23213

Another option would be to setup your UIViewController interactions in your Storyboard using segues and then programmatically call the segue. If you are not sure how to setup a segue then take a look at this tutorial by Ray Wenderlich or search for one; there are plenty out there and anything I write would pale by comparison.

Since everything is configured in the storyboard, the code is simple. This is my example of viewController1 making the call to switch to viewController2.

[self performSegueWithIdentifier: @"DoNumber2" sender:self];

Upvotes: 0

Axeva
Axeva

Reputation: 4737

The general approach is:

    [ParentViewController.view addSubview: ChildViewController.view];
    [ParentViewController addChildViewController: ChildViewController];
    [ChildViewController didMoveToParentViewController: ParentViewController];

In your case, it would be:

    [self.view addSubview: sv.view];
    [self addChildViewController: sv];
    [sv didMoveToParentViewController: self];

Also see the View Controller Containment article on Apple.com.

Upvotes: 0

Greg
Greg

Reputation: 25459

Replace your code with:

UIViewController *sv=[[signUpView alloc]init];
[self addChildViewController:sv];
[self.view addSubview:sv.view];
[sv didMoveToParentViewController:self];

I don't put any comment because this code is self explanatory. That should help.

Upvotes: 1

Related Questions