Passing variables between view controllers

后端 未结 3 1299
温柔的废话
温柔的废话 2020-12-03 08:20

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

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

I would like to

3条回答
  •  既然无缘
    2020-12-03 08:58

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

提交回复
热议问题