Quick Simple Question (Passing values between classes)

Discussion in 'iOS Programming' started by Fuzzball27, Dec 22, 2011.

  1. Fuzzball27 macrumors member

    Fuzzball27

    Joined:
    Aug 8, 2011
    #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?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. Fuzzball27 thread starter macrumors member

    Fuzzball27

    Joined:
    Aug 8, 2011
    #3
    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"?
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Search this forum for "sharing values" and/or "data persistence" and you'll find plenty of threads that discuss this issue and possible solutions.
     
  6. Fuzzball27 thread starter macrumors member

    Fuzzball27

    Joined:
    Aug 8, 2011
    #6
    How do I access an instance?
     

Share This Page