Pergunta

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?

Foi útil?

Solução

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];
    }

Outras dicas

Your problem is that you're assuming the program will intrinsically know where to find the, already laid out, view for this controller when that's simply not how storyboards work. The code you list about will create a view controller, but without an associated view it will simply show as a black square.

There's a few ways to solve your dilemma:

  • Add the modal transition as a segue in the view controller, this would be the simplest way and is what iOS storyboards expect you to do.
  • Move the view from the storyboard to an external .xib and call the initWithNibName:bundle: method to load this as your view controller's view. This is the best solution if you just want to programmatically load the view.
  • Load the view from your storyboard programmatically with the instantiateViewControllerWithIdentifier: method, this is probably a bad idea as it goes against the design of storyboards.

I can elaborate on those if you want.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top