2012-07-12 48 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

相关问题