1

In my app, I need to go to another UIViewController with a button click, but when I did it in the new UIViewController it displays only what I set programmatically.

I used:

NewSubject *NewS = [[NewSubject alloc] initWithNibName:nil bundle:nil];
[self presentViewController:NewS animated:YES completion:nil];

"NewSubject" is the UIViewController I need to go too, however I want the computer to display also the stuff I set by the Storyboard.

Popeye
  • 11,839
  • 9
  • 58
  • 91
  • 2
    Use the storyboard to instantiate your view controller if you want the storyboard configuration of it. (`instantiateViewControllerWithIdentifier:`) – Phillip Mills Jun 05 '14 at 12:53
  • use this link this is hope for u http://stackoverflow.com/questions/23102978/swrevealviewcontroller-without-using-navigationcontroller/23105142#23105142 – Anbu.Karthik Jun 05 '14 at 12:58

2 Answers2

1

Have you set in the Storyboard, in the NewSubject View Controller, in the third tab (Show Identity Inspector) the StoryBoard ID?

You should set it to some name, such as "NewSubject" and use it as follow:

NewSubject *NewS = [self.storyboard instantiateViewControllerWithIdentifier:@"NewSubject"];
[self.navigationController pushViewController:NewS animated:YES];
Alexandre
  • 349
  • 6
  • 10
  • How will this help? They are using storyboards not nibs. So they need to use `instantiateViewControllerWithIdentifier:` – Popeye Jun 05 '14 at 13:24
  • I would probably do `NewSubject *newS = (NewSubject *)[self.storyboard instantiateViewControllerWithIdentifier:@"NewSubject"];` note the lowercase of the `newS` variable, this is sticking to conventions and note the cast to `NewSubject` class. – Popeye Jun 05 '14 at 13:58
  • No problem, glad I could help =). And Popeye, I agree, just wanted to stick to his code, but I should pay more attention in the next answers, thank you for the tip. – Alexandre Jun 05 '14 at 16:24
0

I want the computer to display also the stuff I set by the Storyboard.

If you're using a storyboard, -initWithNibName:bundle: is the wrong method to use. You can use UIStoryboard's -instantiateViewControllerWithIdentifier: method to create a new view controller that's defined in a storyboard, but the more typical approach is to have your button trigger a segue between the two view controllers.

Try this:

While editing your storyboard, control-drag from your button to the new view controller. A popup menu should appear that lets you choose how you want to transition between the view controllers -- push (push the new controller onto the top of the navigation stack), modal (present the view controller modally), etc. Pick the appropriate one.

In simple cases, you're done -- there's no need to write any code just to get the transition to happen. The segue takes care of creating the new view controller and performing the transition for you. However, you often want to pass some data from the existing view controller to the new one. If that's the case, implement -prepareForSegue:sender: in the existing view controller -- this method gives you a chance to pass whatever data you need. It'll look something like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // It doesn't hurt to check that it's the segue that you expect
    if ([segue.identifier isEqualToString:@"MySegueIdentifier"]) {
        NewViewController *newVC = segue.destinationViewController;
        // This is your chance to set properties or call methods to pass data to the new view controller
        newVC.foo = self.foo;
        newVC.bar = self.bar;
    }
}
Caleb
  • 124,013
  • 19
  • 183
  • 272