0

I am new to story boards and I am trying to switch views on a condition but all I am getting is a "black" screen

before , I used to do that :

 if (x==1){

        classView *theView = [[classView alloc] initWithNibName:nil bundle:nil];
        [self presentModalViewController:theView animated:YES]; 
    }

how can I write this in "storyboard"

Thanks

user1051935
  • 579
  • 1
  • 10
  • 29

1 Answers1

1

Create a segue by control dragging from a button in the first view to the new view. Click on the segue and use the attribute inspector to set the style to modal and set the identifier to theView. If you don't need the button in your first view add one anyway, create the segue, and then make the button invisible or place it out of the visible area.

Your code then becomes:

if (x==1) {
    [self performSegueWithIdentifier: @"theView" sender: self];
}
T.J.
  • 3,942
  • 2
  • 32
  • 40
  • Thank you for your answer , I don t get the black screen anymore , however , I don t get to the new view , any reasons ? – user1051935 Jan 17 '12 at 01:57
  • Are both views in storyboard View Controllers? Have you set a breakpoint to be sure the "[self performSegueWithIdentifier: @"theView" sender: self];" is executed? Is the identifier set exactly the same in the code and on the segue in storyboard? Have you set the custom class of the view controller in storyboard? – T.J. Jan 17 '12 at 02:12
  • yes , yes , yes . I found some other posts where I saw that use the function: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender I am using it and it gets called .... – user1051935 Jan 17 '12 at 02:15
  • Great. I thought that method was optional. – T.J. Jan 17 '12 at 02:23
  • Can you try adding another view controller to it to the storyboard, just add a uilabel to it and see if you can segue to that the way I described? – T.J. Jan 17 '12 at 02:36
  • I added an "invisible button" what i did is - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { classView *theView = [[classView alloc] initWithNibName:nil bundle:nil]; [self presentModalViewController:theView animated:YES]; } } if ([[segue identifier] isEqualToString:@"theView"]) { – user1051935 Jan 17 '12 at 02:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6774/discussion-between-t-j-and-user1051935) – T.J. Jan 17 '12 at 03:04
  • After sleeping on it, it seems the issue may be that you are using a view controller class that was written to interface to a XIB and not a storyboard view controller. They are different. If that is the case see this previous post of mine http://stackoverflow.com/questions/8208499/how-to-import-files-from-an-existing-proj-xcode/8369091#8369091. – T.J. Jan 17 '12 at 20:03
  • actually I just found the solution :) http://stackoverflow.com/questions/8897418/unable-to-switch-views-programmatically-using-a-storyboard I didn t have a navigation controller between the two views ... case solved :) – user1051935 Jan 17 '12 at 20:52
  • Glad its working. If the segue style is set to modal you don't need a navigation controller. – T.J. Jan 17 '12 at 23:59