12

I am using this function to switch between views in Xcode 4.3.

[self performSegueWithIdentifier:@"NextView" sender:self];

I would like to pass some parameters between pages. How can I do that?

Pfitz
  • 7,336
  • 4
  • 38
  • 51
Bob
  • 8,392
  • 12
  • 55
  • 96

3 Answers3

39

After performSegueWithIdentifier:sender: your view controler will call

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

assuming your new view controller has some propertys to set:

if ([[segue identifier] isEqualToString:@"NextView"]) {
    MyViewController *myVC = [segue destinationViewController];
    myVC.propertyToSet = // set your properties here
}
Pfitz
  • 7,336
  • 4
  • 38
  • 51
  • so, I set the property like you said, and how can I read value from that property. And also, what if I need to pass multiple variables to the next view? – Bob Jul 20 '12 at 10:51
  • For multiple variables use multiple properties and to read from a property use ... = self.property (in your view controller where you defined that property) – Pfitz Jul 20 '12 at 11:04
  • I understand that, but I am not sure how to get that data in the "NextView". – Bob Jul 20 '12 at 11:11
  • Along the lines of Pfitz's approach, I create a method in my destination view controller to handle any initialization or customization. For example, in Pfitz's example, substitute myVC.propertyToSet with `[myVC setProperty1:self.property1 viewTitle:@"My Title" delegate:self];` – johnnyspo Jul 20 '12 at 11:22
  • Okay, I got that. I am just confused how can I access that variable in the NextView. Should I define a property with the same name and it will be automatically set? – Bob Jul 20 '12 at 12:03
  • In NextView use self.property e.g self.title – Pfitz Jul 20 '12 at 12:05
5

This question has a very nice and correct explanation of passing data between view controllers:

Passing Data between View Controllers

Community
  • 1
  • 1
Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41
  • 1
    yes it is nice explained on that page, but they are using pushViewController for view change, but I would like to use performSegueWithIdentifier. – Bob Jul 20 '12 at 10:47
3

Here is the answer from Pfitz, but I made it a little easier to understand for folks who are new to Objective-C using an example:

  1. In your destinationViewController.h file add property setting for variable to 'receive' value from your source controller.m file

    @property (nonatomic) int billIdReceivingVote;
    
  2. Import your destinationViewController.h to your sourceViewController.m file:

    // VOLViewController.m
    #import "VOLVoteViewController.h
    
  3. Add prepareForSegue method to your sourceViewController.m file and pass the variables

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        VOLVoteViewController *myDestinationViewController = [segue destinationViewController];
    
        // myDestinationViewController.variable = sourceViewController variabe
        myDestinationViewController.billIdReceivingVote = [self.idOfBillSelected intValue];
    }
    
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
tmr
  • 1,500
  • 15
  • 22