1

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];
}
user578386
  • 1,061
  • 3
  • 14
  • 37
  • 1
    For passing data to "QuizModalViewController" you need to either create property in "QuizModalViewController" class or need to create public method – Kapil Kumar Jul 12 '12 at 12:36
  • possible duplicate of [iOS MVC - How to pass data from model to controller?](http://stackoverflow.com/questions/11388888/ios-mvc-how-to-pass-data-from-model-to-controller) – Parag Bafna Jul 12 '12 at 12:47
  • http://stackoverflow.com/questions/9016680/how-many-ways-to-pass-share-data-b-w-view-controller – Parag Bafna Jul 12 '12 at 12:49

3 Answers3

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

Note that theDataYouWantToPass must be a property declared in the QuizModalViewController.h file.

Eugene
  • 10,006
  • 4
  • 37
  • 55
2

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

  • 1
    I don't think `prepareForSegue` gets fired when using `presentModalViewController` since it's a programmatic way of doing a storyboard segue. – Clifton Labrum Jan 05 '14 at 06:23
0

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.

Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • could u gimme a small example – user578386 Jul 12 '12 at 12:33
  • You should be able to find enough examples on SO, like here http://stackoverflow.com/questions/8706281/pass-data-back-to-the-previous-controller or there http://stackoverflow.com/questions/8788294/how-to-access-to-parentviewcontroller-after-presentmodalviewcontroller – Krumelur Jul 12 '12 at 12:34
  • The question is pretty much how to pass the data TO modal view controller, not how to get it from it. So delegation is not the answer here. Custom init methods or properties is. – Eugene Jul 12 '12 at 12:36
  • He might not be leaking since he's probably using ARC – Dustin Jul 12 '12 at 12:37