SegmentedControl not working

Discussion in 'iOS Programming' started by ws47, Jul 25, 2010.

  1. ws47 macrumors newbie

    Jun 30, 2009
    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:
    	[polyView setNeedsDisplay];
    and here is my drawRect:
    //set up the path
    [[UIColor blackColor]setStroke];	
    [[UIColor yellowColor]setFill];
    CGContextSetLineWidth(context, 2);
    if (segmentedControlLineType.selectedSegmentIndex == 0) {
    	//draw solid lines
    if (segmentedControlLineType.selectedSegmentIndex == 1) {
    	//draw dashed lines
    	float lengths[2] = {10, 10};
    	CGContextSetLineDash(context, 0, lengths, 1);
    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. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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. DennisVar macrumors newbie

    Jun 21, 2010
    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. ws47 thread starter macrumors newbie

    Jun 30, 2009
    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. robbieduncan Moderator emeritus


    Jul 24, 2002
    Is it connected to a property with attribute retain? If not then that's probably the problem...
  6. ws47 thread starter macrumors newbie

    Jun 30, 2009
    It is now, but that doesn't solve the problem.

Share This Page