-1

I read many topics here but don't understand. For example I have two ViewControllers. VC1 and VC2. How can I go from VC1 to VC2 programmatically? I loading data with NSURLConnection, then parse it, and if in received data I see string "type=1", I have to run another ViewController, and it has to run all its methods (ViewDidLoad and so on). How can I do it?

In VC1 I have:

If ([[_typeArr objectAtIndex:0]] intValue] == 1)
   //Here I must to run VC2
daleijn
  • 718
  • 13
  • 22

5 Answers5

1

First you need to Add your #import VC2.h file in VC1.m file and Write following code when

if ([[_typeArr objectAtIndex:0]] intValue] == 1)
{
   VC2 *vc2 = [[VC2 alloc] init]; // create object of VC2
   [self presentViewController:vc2 animated:YES completion:nil]; 
            OR // if you have to use navigation controller then
   [self.navigationController pushViewController:vc2 animated:YES];
}
Jakub
  • 13,712
  • 17
  • 82
  • 139
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Thanks, can you answer one another question. I'm not sure, should I ask it like another question, cuz it rather simple. How can I transfer variable from VC1 to VC2. I'm trying do it so: in VC1.h i wrote `+(NSInteger *) globalID;` In VC1.m: `if ([[_typeArr objectAtIndex:0]] intValue] == 1) { VC2 *vc2 = [[VC2 alloc] init]; // create object of VC2 [self presentViewController:vc2 animated:YES completion:nil]; ` – daleijn Nov 14 '13 at 12:32
  • @user2987175 - read this http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers and https://www.google.co.in/search?q=how+to+pass+data+from+one+viewcontroller+to+another+&oq=how+to+pass+data+from+one+viewcontroller+to+another+&aqs=chrome..69i57j0l4.12524j0j7&sourceid=chrome&espv=210&es_sm=91&ie=UTF-8#es_sm=91&espv=210&q=how+to+pass+nsstring++from+one+viewcontroller+to+another – iPatel Nov 14 '13 at 12:37
0

Following is the code for pushing a new viewcontroller onto navigation controller stack. Hope this is what you are looking for.

UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
HRM
  • 2,097
  • 6
  • 23
  • 37
0

That depends - if you build your app using storyboards you should fire a seque with method:

performSegueWithIdentifier:.

If you're not using storyboard and you are using UINavigationController you should fire by:

[self.navigationController pushViewController:viewController animated:YES];

Are you are not using NavigationController maybe UIModalViewController is right answer for you.

Thats really depends.. but i highly recommend you to use storyboard and segue. There are pretty great, and connections to UINavigationController is created itself.

Jakub
  • 13,712
  • 17
  • 82
  • 139
0
1. If your Application is using Navigation controller Without Storyboard try this:

if ([[_typeArr objectAtIndex:0]] intValue] == 1)
{
    VC2 *vc2 = [[VC2 alloc] initWithNibName:@"VC2"]; // create object of VC2
    [self.navigationController pushViewController:vc2 animated:YES];
}

2.If your Project is not using neither Navigation controller nor Storyboard try this:
 if ([[_typeArr objectAtIndex:0]] intValue] == 1)
   {
     VC2 *vc2 = [[VC2 alloc] initWithNibName:@"VC2"]; // create object of VC2
     [self presentViewController:vc2 animated:YES completion:nil]; 
}

3.If your Project is using Navigation controller and Storyboard Follow This Step:

      1.In Storyboard File > Go to Your First View Controller on left Side  >> Right Click >> Give   Connection From Manual to Second View Controller (Under Trigger Segue)

As Result Connection is created With Arrow In Circle 

Click On Arrow Button (If you can not see that arrow Zoom out  storyboard file) 

On Right Side Click on Attribute Inspector 

Give Identifier Name "VC2"


Now in Code Part:

if ([[_typeArr objectAtIndex:0]] intValue] == 1)
{
   [self performSegueWithIdentifier:@"VC2" sender:nil]; // Must Be Same as given In Identifier field in storyboard
}
-1

just look a tutorial for navigation controller as you need a container controller which can hold the navigation from one to other, here are some links to go and come back with specific problem if you got.

http://www.ralfebert.de/archive/ios/tutorial_iosdev/navigationcontroller/

http://www.techotopia.com/index.php/Creating_a_Navigation_based_iOS_5_iPhone_Application_using_TableViews

http://www.appcoda.com/use-storyboards-to-build-navigation-controller-and-table-view/

Retro
  • 3,985
  • 2
  • 17
  • 41