user578386
user578386

Reputation: 1061

How to pass data between modal view controller

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

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

Upvotes: 1

Views: 7167

Answers (3)

Christian Pappenberger
Christian Pappenberger

Reputation: 359

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

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....

Upvotes: 2

Eugene
Eugene

Reputation: 10045

-(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 theDataYouWantToPass must be a property declared in the QuizModalViewController.h file.

Upvotes: 4

Krumelur
Krumelur

Reputation: 33068

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

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

Upvotes: 0

Related Questions