1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

SegmentedControl not working

Discussion in 'iPhone/iPad Programming' started by ws47, Jul 25, 2010.

  1. macrumors newbie

    #1
    I'm working on the extra credit of assignment 3 on Stanford's app development class. I'm trying to do the part where you use a segmented control to choose solid or dashed lines. I added an outlet for the segmented control to my view controller. When the value of the control is changed, it calls this method in the controller:
    Code:
    -(IBAction)segmentedControlLineTypeChanged{
    	[polyView setNeedsDisplay];
    }
    and here is my drawRect:
    Code:
    //set up the path
    CGContextClosePath(context);
    [[UIColor blackColor]setStroke];	
    [[UIColor yellowColor]setFill];
    CGContextSetLineWidth(context, 2);
    if (segmentedControlLineType.selectedSegmentIndex == 0) {
    	//draw solid lines
            NSLog(@"solid");
    }
    if (segmentedControlLineType.selectedSegmentIndex == 1) {
    	//draw dashed lines
    	float lengths[2] = {10, 10};
    	CGContextSetLineDash(context, 0, lengths, 1);
            NSLog(@"dashed");
    }
    CGContextDrawPath(context, kCGPathFillStroke);
    
    For some reason, it always draws solid lines and never dashed ones. The code for dashed lines is never run because "dashed" never shows up in the console. Any ideas?
     
  2. macrumors 603

    #2
    Suppose segmentedControlLineType is nil. What would happen? How would that be possible?

    Learn to use the debugger to set a breakpoint. Set the breakpoint on your drawRect method. Inspect the relevant variables. Trust me, learning how to do these things will be of incalculable benefit.

    You cannot avoid debugging. You can, however, avoid learning how to use the best tools for debugging. It's better to learn how to use the debugger on simple programs, rather than complex ones.
     
  3. macrumors newbie

    #3
    I'm sure you already looked at this, but have you actually hooked up the segmentedControlLineType via an IBOutlet in Interface Builder? as chown33 points out, segmentedControlLineType may be nil here...
     
  4. macrumors newbie

    #4
    All my connections are done correctly. I used to debugger and saw the segmentedControlLineType is nil (it has a value of 0x0), which is what is causing my problem. I still can't figure out why it is nil though.
     
  5. Moderator

    robbieduncan

    Staff Member

    #5
    Is it connected to a property with attribute retain? If not then that's probably the problem...
     
  6. macrumors newbie

    #6
    It is now, but that doesn't solve the problem.
     

Share This Page