2013-11-14 62 views
-1

我在這裏閱讀了很多主題,但不明白。例如,我有兩個ViewController。 VC1和VC2。我怎樣才能以編程方式從VC1到VC2?我用NSURLConnection加載數據,然後解析它,如果在接收到的數據中看到字符串「type = 1」,我必須運行另一個ViewController,並且必須運行它的所有方法(ViewDidLoad等)。我該怎麼做?如何從代碼中調用另一個ViewController?

在VC1我:

If ([[_typeArr objectAtIndex:0]] intValue] == 1) 
    //Here I must to run VC2 

回答

1

首先,你需要添加您#import VC2.h文件VC1.m文件和 寫下面的代碼時

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

謝謝,你能回答另一個問題。我不確定,我是否應該像另一個問題那樣提問,因爲它很簡單。如何將變量從VC1傳輸到VC2。我試圖做到這一點:在VC1.hi寫了'+(NSInteger *)globalID;'在VC1.m中:if([[_typeArr objectAtIndex:0]] intValue] == 1) { VC2 * vc2 = [[VC2 alloc] init]; //創建VC2的對象 [self presentViewController:vc2 animated:YES completion:nil]; ' – daleijn

+0

@ user2987175 - 閱讀此http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers和https://www.google.co.in/search?q=how+to+pass +數據+從+一個+的ViewController +到+另一個+ OQ =如何+傳遞+數據+從+一個+的ViewController +到+另一個+ AQS = chrome..69i57j0l4.12524j0j7&的SourceID =鉻&espv = 210&es_sm = 91&即= UTF- 8#es_sm = 91&espv = 210 q =如何+通過+的NSString ++從+ +一個+的ViewController至+另一個 – iPatel

0

以下爲推一個新視圖 - 控制到導航控制器堆棧的代碼。希望這是你正在尋找的。

UIViewController *viewController = [[[UIViewController alloc] init] autorelease]; 
[self.navigationController pushViewController:viewController animated:YES]; 
0

這取決於 - 如果你使用故事板來構建你的應用程序,你應該使用方法觸發seque:

performSegueWithIdentifier:

如果你不使用故事板和你正在使用的UINavigationController你應該火借:

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

你不使用NavigationController也許UIModalViewController是你正確的答案。

這真的取決於..但我強烈建議你使用故事板和segue。這裏非常棒,並且自己創建了與UINavigationController的連接。

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 
} 
相關問題