Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fuzzball27

macrumors member
Original poster
Aug 8, 2011
30
1
Hello,
I would like to know how I can pass a value between two classes. For example, I would like ClassOne to get the value of the double "dVal" from ClassTwo. How can I do this?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You question indicates a significant problem in your understanding of object-oriented programming. You do not pass values between classes. You pass them between objects which are instances of classes. The normal way is to define a message that allows you to do this: one instance sends a message that the second instance understands with the value or values as a parameter. One suggestion for implementation is to define a property which you can set by sending a message from the instance with the value to the instance you want to have the value.
 

Fuzzball27

macrumors member
Original poster
Aug 8, 2011
30
1
I apologize for the lack of clarity. I will post my code to restate the question.
Code:
@interface SimonFirstViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *firstWindow;
@property (weak, nonatomic) IBOutlet UIButton *yellowButton;
@property (weak, nonatomic) IBOutlet UIButton *orangeButton;
@property (weak, nonatomic) IBOutlet UIButton *redButton;
@property (weak, nonatomic) IBOutlet UIButton *greenButton;
@property (weak, nonatomic) IBOutlet UIButton *blueButton;
- (IBAction)yellowPressed:(id)sender;
- (IBAction)orangePressed:(id)sender;
- (IBAction)redPressed:(id)sender;
- (IBAction)greenPressed:(id)sender;
- (IBAction)centerPressed:(id)sender;
@end
This is the method that gives me an error:
Code:
- (void)dealWithBackgroundColor {
    double color = [SimonSecondViewController getColor];
    if (color == 0) {
        firstWindow.backgroundColor = [UIColor  whiteColor];
    }
    else if(color == 1) {
        firstWindow.backgroundColor = [UIColor  grayColor];
    }
    else if(color == 2) {
        firstWindow.backgroundColor = [UIColor  blackColor];
    }
}
Here is the second view class info:
Code:
@interface SimonSecondViewController : UIViewController
{
    double difficulty, color;
}

@property (weak, nonatomic) IBOutlet UIStepper *difficultyStepper;
@property (weak, nonatomic) IBOutlet UIStepper *colorStepper;
@property (weak, nonatomic) IBOutlet UISlider *SoundEffectsVolumeSlider;
@property (weak, nonatomic) IBOutlet UILabel *difficultyLabel;
@property (weak, nonatomic) IBOutlet UILabel *colorLabel;
- (IBAction)changeDifficulty:(id)sender;
- (IBAction)changeBackgroundColorInGame:(id)sender;
@end
Methods in question:
Code:
- (IBAction)changeBackgroundColorInGame:(UIStepper *)sender {
    double stepperValue = [sender value];
    NSString *colorText;
    if (stepperValue == 0) {
        colorText = @"White";
    }
    else if (stepperValue == 1) {
        colorText = @"Grey";
    }
    else if (stepperValue == 2) {
        colorText = @"Black";
    }
    self.colorLabel.text = colorText;
    color = [sender value];
}

- (double)getColor {
    return color;
}
This line "double color = [SimonSecondViewController getColor];" doesn't access the getColor method in SimonSecondViewController. How can I get the return value in the method "dealWithBackgroundColor" from "SimonFirstViewController"?
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
Class methods begin with a +, not a -, but like the previous person said, you should be accessing an instance of your class, not the class itself.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Search this forum for "sharing values" and/or "data persistence" and you'll find plenty of threads that discuss this issue and possible solutions.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.