2012-07-12 22 views
1

這之間的數據是一個菜鳥,我想的疑問是通過從我QuizViewcontroller到QuizModalViewController.i數據成功地創建一個正常的模態視圖控制器,但兩者之間傳遞數據時有問題。 。以下是我的代碼。如何通過模式視圖控制器

QuizViewController

-(IBAction)button3Clicked:(id)sender 
{ 
    QuizModalViewController *mvid=[[QuizModalViewController alloc]initWithNibName:@"QuizModalViewController" bundle:nil]; 
mvid.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:mvid animated:YES]; 
} 

QuizModalViewController

- (IBAction)goBackToView 
{ 
[self dismissModalViewControllerAnimated:YES]; 
//[controller loadQuestion:currentQuestionIndex+1]; 
} 
+1

對於傳遞到「QuizModalViewController」你需要無論是在「QuizModalViewController」類中創建屬性或需要創建公共方法 – 2012-07-12 12:36:55

+0

數據可能的[iOS MVC的重複 - 如何將數據從模型傳遞到控制器?](http://stackoverflow.com/questions/11388888/ios-mvc-how-to-pass-data-from-model-to-controller) – 2012-07-12 12:47:57

+0

http://stackoverflow.com/questions/9016680/how-many-ways-to-pass-share-data-bw-view-controller – 2012-07-12 12:49:29

回答

4
-(IBAction)button3Clicked:(id)sender 
{ 
    QuizModalViewController *mvid=[[QuizModalViewController alloc]initWithNibName:@"QuizModalViewController" bundle:nil]; 
mvid.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
mvid.theDataYouWantToPass = theData; // this is how you do it 
[self presentModalViewController:mvid animated:YES]; 
} 

注意theDataYouWantToPass必須在QuizModalViewController.h文件中聲明的屬性。

+0

這是最好的方法。 – 2012-07-12 12:43:17

+0

'@property(nonatomic)NSInteger myInteger;' – Eugene 2012-07-12 13:04:12

+0

OMG。我無法弄清楚爲什麼這不起作用,最後你的筆記提醒我通過將它們放置在實現文件中來使這些屬性保密。謝謝! – Arel 2015-10-23 03:47:30

0

做的,而不是使用一個局部變量的QuizModalViewController可以在類範圍。 然後在goBackToView:中,您可以在解除控制器的內容之前訪問控制器的內容。

順便說一句:在你的代碼你泄露mvid。以模態形式呈現後釋放它。

+0

可以ü給我一個小例子 – user578386 2012-07-12 12:33:05

+0

你應該能夠找到足夠的例子,就像這裏http://stackoverflow.com/questions/8706281/pass-data-back-to-the-previous-controller或那裏http://stackoverflow.com/questions/8788294/how-to-access-to-parentviewcontroller-after-presentmodalviewcontroller – Krumelur 2012-07-12 12:34:32

+0

問題是如何將數據傳遞到模態視圖控制器,而不是如何從中獲取它。所以代表團並不是這裏的答案。自定義的init方法或屬性是。 – Eugene 2012-07-12 12:36:41

2

您還可以使用 - (空)prepareForSegue:... - 方法控制器之間共享數據。

例如:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

    if ([segue.identifier isEqualToString:@"Your_Identifier"]) { 

    TestViewController *controller = [segue destinationViewController]; 

    NSArray *array = [[NSArray alloc] initWithObjects:@"",nil]; 

    controller.objectInTestViewController = array; 

    } 
} 

希望這可以幫助別人....

+1

我不認爲'prepareForSegue'會在使用'presentModalViewController'時被解僱,因爲它是一個編寫故事板segue的程序化方法。 – 2014-01-05 06:23:55

相關問題