Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ws47

macrumors newbie
Original poster
Jun 30, 2009
7
0
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,739
8,415
A sea of green
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

macrumors newbie
Jun 21, 2010
28
0
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?

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

macrumors newbie
Original poster
Jun 30, 2009
7
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.