Search code examples
iosipadcocoa-touchuiviewcontrolleruimodalpresentationstyle

My viewController doesn't show up right


in my iPad-app I am trying to present one of my views with a modal formsheet-style. Here's some code:

-(void)present
{
    SecondViewController *modal = [[SecondViewController alloc]init];
    modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
    [self presentModalViewController:modal animated:YES];
}

I am using Storyboard, and I have put stuff like a textView and toolbars in the view I'd like to show. I have set the right class in Identity Inspector, and in the class-files I have checked that it's the right view appearing with putting NSLog(@"Right view");

When calling the void present, a view is appearing, but only as a dark-white square. Nothing og my content from Storyboard is in it, I even tried changing the background color of the view and the textView to see if something was just outside the square, but the whole thing stayed white. It feels like it's not using the view I created in storyboard, but I have set it to the correct class, and the NSLog gets printed out when calling it. I have not connected the two views in any way in Storyboard, the SecondViewController is just floating around, so that might be the problem? The button that calls for -(void)present is created programmatically, so I can't ctrl+drag it to the button either.

Why is it showing an empty version of my class?


Solution

  • In the "Identity Inspector" set a "Storyboard ID" for your ViewController, and then present it like this:

    -(void)present
    {
        SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:@"myStoryboardID"];
        modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
        [self presentModalViewController:modal animated:YES];
    }
    

    And if you're using iOS6, presentModalViewController:animated: is deprecated, so use this:

     -(void)present
        {
            SecondViewController *modal = [self.storyboard instantiateViewControllerWithIdentifier:@"myStoryboardID"];
            modal.modalPresentationStyle = UIModalPresentationStyleFormSheet;
            [self presentViewController:modal animated:YES completion:nil];
        }