PDA

View Full Version : SegmentedControl not working




ws47
Jul 25, 2010, 03:52 PM
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:
-(IBAction)segmentedControlLineTypeChanged{
[polyView setNeedsDisplay];
}
and here is my drawRect:
//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?



chown33
Jul 25, 2010, 04:19 PM
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.

DennisVar
Jul 26, 2010, 02:29 AM
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:
-(IBAction)segmentedControlLineTypeChanged{
[polyView setNeedsDisplay];
}
and here is my drawRect:
//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?

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...

ws47
Jul 26, 2010, 09:20 AM
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.

robbieduncan
Jul 26, 2010, 09:25 AM
Is it connected to a property with attribute retain? If not then that's probably the problem...

ws47
Jul 26, 2010, 12:26 PM
Is it connected to a property with attribute retain? If not then that's probably the problem...

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