Need default value to appear when using a switch statement

Discussion in 'iOS Programming' started by StevenHu, Feb 4, 2010.

  1. StevenHu macrumors member

    Joined:
    Sep 3, 2009
    Location:
    Southern CA
    #1
    I am using segmented controls and a slider on a view. In most cases, a default value of the control is appearing in a text box when the view controller is first accessed. (These are all created by code, not IB (based on Apple's UICatalog code). But in two cases they are left blank.

    In the ViewDidLoad section, I have the following methods that work well except the default value does not appear. I don't know what syntax to use. Can you help?

    Thanks,
    Steve

    For the slider:
    Code:
    - (void)sliderActionRideHeight:(id)sender{
    if (labelRideHeight.text) // Don't know how to word this line correctly
    	{
    	 NSArray *arrayRideHeight = [NSArray arrayWithObjects: @"15.0", @"15.5", @"16.0", @"16.5", @"17.0", @"17.5", nil];
    	 
    	NSString *labelValueRideHeight;
    	labelValueRideHeight = [arrayRideHeight objectAtIndex:(int)sliderRideHeight.value]; // To link slider value to object in array and echo to label.
    	labelRideHeight.text = [NSString stringWithFormat:@"%@mm", labelValueRideHeight]; // from a forum post reply
    	}
    
    else { labelRideHeight.text = [NSString stringWithFormat:@"%@mm", 25.0]; }  // Default value. Does not work
    
    }
    For the segmented control with three buttons (buttons contain images):
    Code:
    - (void)segmentAction:(id)sender
    {
    
    	switch ([((UISegmentedControl *)sender) selectedSegmentIndex]) 
    	{
    		case 0:
    			labelBumpSteer.text = @"0 washers"; 
    			break;
    		case 1:
    			labelBumpSteer.text = @"1 washer";
    			break;
    		case 2:
    			labelBumpSteer.text = @"2 washers";
    			break;
    		default:
    			labelBumpSteer.text = @"0 washers"; // Default value. Does not work.
    			break;
    	}
    	return;
    }
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    It doesn't work because 25.0 is a floating-point number, not an object. Don't use a format. Or use @"25.0" instead of 25.0.
     
  3. StevenHu thread starter macrumors member

    Joined:
    Sep 3, 2009
    Location:
    Southern CA
    #3
    You mean
    Code:
    [NSString:@"25.0mm"];
    That doesn't work either, sorry.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Don't panic. Think it through.

    You have this:
    Code:
    labelRideHeight.text = [NSString stringWithFormat:@"%@mm", 25.0];
    
    You should be assigning a constant string object. So something like:
    Code:
    labelRideHeight.text = somethingThat'sAConstantStringObject;
    
    What part of the code you posted is a constant string object?
     
  5. StevenHu thread starter macrumors member

    Joined:
    Sep 3, 2009
    Location:
    Southern CA
    #5
    That would be 25.0mm.

    So this runs without errors:
    Code:
    { labelRideHeight.text = @"45.0"; }
    Hmmm ... still not showing up in the label. Here's the coding for the label:

    Code:
    	yPlacement += kTweenMargin + kLabelHeight;
    	rect = CGRectMake(kResultsLeftMargin, // lower number moves left margin to right.
    					  yPlacement-35, // higher neg. number raises it.
    					  kImageWidth+40,
    					  kLabelHeight+5);
    	labelRideHeight = [[UILabel alloc] initWithFrame:rect];
    	labelRideHeight.backgroundColor = [UIColor whiteColor];
    	labelRideHeight.font = [UIFont systemFontOfSize:14];
    	labelRideHeight.textAlignment = UITextAlignmentLeft;
    	labelRideHeight.textColor = [UIColor blueColor];
    	[self.view addSubview:labelRideHeight];
     

Share This Page