xcode 如何在模态视图控制器之间传递数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11451845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:52:10  来源:igfitidea点击:

How to pass data between modal view controller

objective-cxcode

提问by user578386

This is a noob question.what i want is to pass data from my QuizViewcontroller to QuizModalViewController.i was successful in creating a normal modal view controller but having problem when passing data between the two..below is my code.

这是一个菜鸟问题。我想要的是将数据从我的 QuizViewcontroller 传递到 QuizModalViewController。我成功地创建了一个普通的模态视图控制器,但是在两者之间传递数据时遇到了问题......下面是我的代码。

QuizViewController

测验视图控制器

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

QuizModalViewController

QuizModalViewController

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

回答by Eugene

-(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];
}

Note that theDataYouWantToPassmust be a property declared in the QuizModalViewController.hfile.

注意theDataYouWantToPass必须是QuizModalViewController.h文件中声明的属性。

回答by Christian Pappenberger

You can also use the -(void)prepareForSegue:... - Method to share data between controllers.

您还可以使用 -(void)prepareForSegue:... - 方法在控制器之间共享数据。

For Example:

例如:

- (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;

   }
}

Hope this helps someone....

希望这可以帮助某人....

回答by Krumelur

Make your QuizModalViewControlleravailable at class scope instead of using a local variable. Then in the goBackToView:you can access the controller's content prior to dismissing it.

使您QuizModalViewController在类范围内可用,而不是使用局部变量。然后,goBackToView:您可以在关闭控制器之前访问控制器的内容。

By the way: in your code you're leaking mvid. Release it after presenting it modally.

顺便说一句:在您的代码中,您正在泄漏mvid. 模态呈现后释放它。