14

I have 2 view controllers in my project. Inside View Controller1 I want to switch to View Controller 2 by press of a button. Currently I do this

- (IBAction)startController2:(id)sender { viewController1 vc2 = [[viewController2 alloc] init]; self.view = vc2.view; } 

This seems to work fine, but there is a big delay (4 secs) between the button press and second view controller appears. If I call the viewController2 directly from the AppDelegate things load faster. What am I doing wrong here. Any help is greatly appreciated.

2
  • it's definitely wrong to move view controllers views' between view controllers like that. (Each view controller "owns" it's view.) Commented Oct 25, 2013 at 5:51
  • 1
    you should use navigation controller to push to another viewcontroller Commented Oct 25, 2013 at 6:24

5 Answers 5

40

Several things to consider.

Part 1: "What am I doing wrong here"?

  1. You definitely didn't mean to do self.view = vc2.view. You just put one view controller in charge of another view controller's view. What you probably mean to say was [self.view addSubview:vc2.view]. This alone might fix your problem, BUT...

  2. Don't actually use that solution. Even though it's almost directly from the samples in some popular iPhone programming books, it's a bad idea. Read "Abusing UIViewControllers" to understand why.

Part 2: What you should be doing

It's all in the chapter "Presenting View Controllers from Other View Controllers".

It'll come down to either:

  • a UINavigationController, (see the excellent Apple guide to them here) and then you simply [navigationController pushViewController:vc2]

  • a "manually managed" stack of modal view controllers, as andoabhay suggests

  • explicitly adding a VC as child of another, as jason suggests

Sign up to request clarification or add additional context in comments.

Comments

4

You should consider using UINavigationController to switch view controllers. If your building target is iOS 5.0+, you can also use the new controller container concept: [mainViewController addChildViewController:childViewController].

Comments

2

Use presentModalViewController as follows

[self presentModalViewController:vc2 animated:YES completion:^(void){}]; 

and in the viewController1 use

[self dismissModalViewControllerAnimated:YES completion:^(void){}]; 

where ever you want to go back to previous controller.

2 Comments

This approach is deprecated now
dismissViewControllerAnimated:completion: is the new way to use this paradigm
0
[aController presentViewController:bController animated:NO completion:nil]; [bController presentViewController:cController animated:NO completion:nil]; 

when you want dismiss cController, you can do like this

[aController dismissViewControllerAnimated:NO completion:nil]; 

this is the flow chart.

aController → bController → cController ↑___________________________↓ 

Comments

-1

You should use UINavigationController to switch view controllers.

You are on View1 and add the following code on button click method.

View2 *View2Controller = [[View2 alloc] initWithNibName:@"View2" bundle:nil]; [self.navigationController pushViewController:view2Controller animated:YES];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.