UISegmented control issue

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

  1. Fuzzball27 macrumors member

    Fuzzball27

    Joined:
    Aug 8, 2011
    #1
    I'm having an issue that I have localized to this block of code:
    Code:
        UIColor *color;
        NSInteger pressed = ViewTwo.background.selectedSegmentIndex;
    //background is an instance of UISegmentedControl
        if (pressed == 0) {
            color = [UIColor whiteColor];
        }
        else if (pressed == 1) {
            color = [UIColor grayColor];
        }
        else if (pressed == 2) {
            color = [UIColor blackColor];
        }
        else color = [UIColor whiteColor];
        self.window.backgroundColor = color;
    My problem is that the background isn't changing colors when the color depends on the segmented control. I have verified that the code is being run; replacing the if statements with "color = [UIColor someColor];" works fine. Does anyone have an ide why my code isn't working?
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    At minimum I would verify a few things. First that ViewTwo and background are not nil here. Then, the value assigned to the pressed variable if the first two items are not nil.
     
  3. Fuzzball27 thread starter macrumors member

    Fuzzball27

    Joined:
    Aug 8, 2011
    #3
    I have verified that ViewTwo is nil. Besides the fact that ViewTwo has no value, I'm not quite sure what that means? How can I fix that?
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    It means that the variable, ViewTwo, does not point to any object.

    To help further, we'd have to understand if & when you set ViewTwo up, and where the code sample above is in relation to that.
     
  5. Fuzzball27 thread starter macrumors member

    Fuzzball27

    Joined:
    Aug 8, 2011
    #5
    View two interface:
    Code:
    @interface SimonSecondViewController : UIViewController
    
    @property (weak, nonatomic) IBOutlet UISegmentedControl *background;
    @end 
    View one interface:
    Code:
    @interface SimonFirstViewController : UIViewController
    {
        SimonSecondViewController *ViewTwo;
    }
    
    @property (weak, nonatomic) IBOutlet UIView *window;
    @end 
    Code:
    
    - (void)setBackgroundColor
    {
        UIColor *color;
        NSInteger pressed = ViewTwo.background.selectedSegmentIndex;
        if (pressed == 0) {
            color = [UIColor whiteColor];
        }
        else if (pressed == 1) {
            color = [UIColor grayColor];
        }
        else if (pressed == 2) {
            color = [UIColor blackColor];
        }
        else color = [UIColor whiteColor];
        self.window.backgroundColor = color;
    } 
    This ^^ is called by ViewDidAppear in the implementation of view one.
     
  6. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    Unless this is an iPad app, you can only have one UIViewController on the screen at a time. I'm not yet familiar with iPad VC (UIViewController) differences. So...

    You can't interact with the interface of a VC that isn't displayed on the screen. You don't show where and if you have instantiated (created an instance) ViewTwo. It sounds like you want to interact with ViewTwo while an instance of SimonFirstViewController is displayed. That isn't possible on an iPhone or iPod.

    Describe in detailed steps what it is you want to do.
     

Share This Page