0

I am using following code to launch a view controller modally. This view controller is not being created from scratch. Rather, I created and styled it in storyboard, gave it a name, embedded it in a navigation controller so it has a navigation bar and created cancel and done buttons, gave it a title and the code below launches it.

The problem is that while most of the features of the screen show up such as labels and images, the title and navigation bar that you can see in the storyboard disappear. Has anyone come across this issue and have a fix for it?

My code to launch VC created in storyboard.

- (void) editView:(id) sender
{
    NSLog(@"launch button pressed");
    UIStoryboard *storyBoard = self.storyboard;
    IDEditVC *editVC =
    [storyBoard instantiateViewControllerWithIdentifier:@"editvc"];
editVC.item = _item;
[self presentModalViewController:editVC animated:YES];
    }
user1904273
  • 4,562
  • 11
  • 45
  • 96
  • You need to instantiate and present the navigation controller, not IDEditVC. – rdelmar Apr 06 '15 at 20:53
  • The question you linked to does not have an accepted answer. If you think this is a duplicate, you have to show a question that was actually answered, not where the answer did not work for the poster. – user1904273 Apr 06 '15 at 21:00
  • Only because the OP didn't implement it correctly. That is the correct way to do it. – rdelmar Apr 06 '15 at 21:00
  • So you know how to implement it correctly but that poster did not? Would you care to share your exceptional knowledge? – user1904273 Apr 06 '15 at 21:01
  • I did share my knowledge in that answer. As I said in my first comment here, you need to instantiate the navigation controller (give it an identifier) and present it. – rdelmar Apr 06 '15 at 21:04
  • I gave it the nav controller an identifier and changed code but app is now crashing – user1904273 Apr 06 '15 at 21:09

2 Answers2

1

I think what's happening is that since you are directly instantiating the view controller by name it's getting just that from the storyboard rather than embedding it in the nav controller.

Try this:

  1. In your storyboard, make the Navigation Controller the initial/root View Controller for the storyboard
  2. Instead of instantiating by name, use UIStoryboard's instantiateInitialViewController method.

EDIT: Based on your comments below, you might want to try this:

- (void) editView:(id) sender
{
    NSLog(@"launch button pressed");
    UIStoryboard *storyBoard = self.storyboard;
    IDEditVC *editVC =
    [storyBoard instantiateViewControllerWithIdentifier:@"editvc"];
    editVC.item = _item;

    UINavigationController *nav = [UINavigationController alloc] initWithRootViewController: IDEditVC];
    // Do whatever setup you want to here for your title bar, etc
    [self presentModalViewController:nav animated:YES];
}
MahatmaManic
  • 943
  • 1
  • 8
  • 16
0

Try to use PushViewController instead of instantiate:

How to push viewcontroller ( view controller )?

Good luck

Community
  • 1
  • 1
Black Magic
  • 2,706
  • 5
  • 35
  • 58