views:

212

answers:

1
@interface UICustomSwitch : UISwitch 
{

}
-(void)setLeftLabelText:(NSString *)labelText;
-(void)setRightLabelText:(NSString *)labelText;
@end
@implementation UICustomSwitch
-(UIView *)slider
{
    return [[self subviews ] lastObject];
}
-(UIView *)textHolder
{
    return [[[self slider] subviews]objectAtIndex:2];
}
-(UILabel *)leftLabel
{
    return [[[self textHolder] subviews]objectAtIndex:0];

}
-(UILabel *)rightLabel
{
    return [[[self textHolder] subviews]objectAtIndex:1];

}
-(void)setLeftLabelText:(NSString *)labelText;
{
    [[self leftLabel] setText:labelText];
}
-(void)setRightLabelText:(NSString *)labelText
{
    [[self rightLabel]setText:labelText];

}
@end

switchView=[[[UICustomSwitch alloc]initWithFrame:CGRectMake(200,5,90,30)]autorelease];
        [switchView setLeftLabelText:@"F"];
        [switchView setRightLabelText:@"M"];
        [switchView addTarget:self action:@selector(genderAction:) forControlEvents:UIControlEventValueChanged];

-(void)genderAction:(id)sender
{
    if([Object.gender isEqualToString:@"F"])
    {
        Object.gender=@"M";


    }
    else
    {
        Object.gender=@"F";

    }
}

It is working fine for adding the details into the object. I am getting Male for 'M' and Female for 'F'. I have one object that has a value that switches between M and F . However, in the UI, the blue color of switch is not displayed. The value in the object is correct but the ON state(blue color) of switch is not displayed.

A: 

You need to call – setOn:animated: for the switch for the approbate value when you initialize it or when the value changes from the data.

If the switch is visible but greyed out and inactive, you need to set it to enabled.

TechZen