0

I am using XCode 4.4 and making an app with storyboard. I want to switch to another view in a method of a view. I want to achieve the same functionality as switching view on storyboard by modal,inside the method. So far I am using the following code:

-(IBAction)xplus:(id)sender
{
    x++;
     if(x>30)
    {
           ViewController_2 *viewControllerx = [[ViewController_2 alloc] init];
         [self presentViewController:viewControllerx animated:YES completion:nil];
     }

 }

When the view is supposed to change ie when x becomes greater than 30 a Black Screen appears instead of the required view. I personally think my code is correct but why it shows a black screen?

How can I to achieve the same functionality as switching view on storyboard by modal,inside the method ?

1 Answers1

0

Your method is not working out probably because you are not not using the .xib file with the view controller that you want to go to and using storyboard.

Anyways let me give you an easy and efficient method to perform the task.

Step 1: Go to storyboard of your project and click on the View Controller to which you want to go in your method i.e. ViewController_2.Click on the inspector icon(It is the 4th one). In the identifier field type a name eg: second.

Step 2: Use the following lines in your method to create a instance of ViewController_2 using the initiateViewControllerWithIdentifier method and to modal to it.

ViewController_2 *v2=[self.storyboard initiateViewControllerWithIdentifier:@"second"]; [self presentModalViewController:v2.animated:YES];

Now the app will go to(create instance of) the ViewController having the same Identifier as the one given as argument i.e. second.

Mike
  • 47,263
  • 29
  • 113
  • 177
Imroze Aslam
  • 103
  • 1
  • 3