0

I wish to transfer float values from one view controller to another. All the tutorials I can find only give examples of text transfers, not values.

I have a settings page which allows a user to click on buttons which have constant float values, such as Tear = 0.13, Engine = 0.15, etc. I wish to transfer these values to the root view controller, once a button is clicked on.

I have created a push segue, and added some code from a tutorial for the prepareforSegue: of the implementation file of the source view controller.

Can anyone help me with the next step, which is the code for transferring float values from the source controller to the destination (root) controller. Here's what I have so far in prepareforSegue:sender: in the source view controller.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier] isEqualToString:@"SendInfo"]) {
         ViewController *detailViewController = [segue destinationViewController];

And this is how the CalcViewController implementation file looks in the IBAction area.

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

        if ([[segue identifier] isEqualToString:@"SendInfo"]) {
            ViewController *detailViewController = [segue destinationViewController];}
}

- (IBAction)WT2:(id)sender {


}

- (IBAction)WT:(id)sender {

    float Tear=0.13;
}

- (IBAction)Car1:(id)sender {

    float Litre=0.13;

}

- (IBAction)Car2:(id)sender {

    float Litre=0.15;
}

- (IBAction)Car3:(id)sender {

    float Litre=0.25;
}

- (IBAction)MP1:(id)sender {

    float Net1=0.03;
}

- (IBAction)MP2:(id)sender {
}

@end
jscs
  • 63,694
  • 13
  • 151
  • 195
user1641906
  • 117
  • 1
  • 2
  • 14

1 Answers1

0

Your problem is that you don't don't store your Tear/Engine/Litre values anywhere. From what I can gather, you want to set a float value on an instance of ViewController. Setting a property on an object is simple.

ViewController.h

@interface ViewController : UIViewController
@property (nonatomic) float litre;
@property (nonatomic) float tear;
@end

CalcViewController.m

@interface CalcViewController ()
@property (nonatomic) float selectedLitre;
@property (nonatomic) float selectedTear;
@end

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"SendInfo"]) {
        ViewController *detailViewController = [segue destinationViewController];
        detailViewController.litre = self.selectedLitre;
        detailViewController.tear = self.selectedTear;
        //present detailViewController here...
    }
}

- (IBAction)WT:(id)sender {
   self.selectedTear = 0.13;
}

- (IBAction)Car1:(id)sender {
    self.selectedLitre = 0.13;
}
Community
  • 1
  • 1
CrimsonChris
  • 4,651
  • 2
  • 19
  • 30
  • I notice that you created an object *detailViewController of ViewController - but the object isn't called in ViewController.h for float values. Is that correct?? – user1641906 Jun 20 '14 at 14:43
  • Hi again, Chris! I implemented your code, but the project has crashed ever since. It seems like a memory leak, but I'm not sure where the problem lies in your code. Any suggestions? It seems that if I make a choice of one of the buttons, for Tear, or Litre, then it crashes.. – user1641906 Jun 25 '14 at 17:05
  • @user1641906 This question has been closed. Please make a new one if you have a _specific_ question about your code. – CrimsonChris Jun 25 '14 at 17:56