0
@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";

    }
}

将细节添加到对象中效果很好。我为“M”获取男性,为“F”获取女性。我有一个对象,它的值在 M 和 F 之间切换。但是,在 UI 中,开关的蓝色不显示。对象中的值正确但不显示开关的ON状态(蓝色)。

4

1 回答 1

0

– setOn:animated:当您初始化它或当值从数据中改变时, 您需要为认可值调用开关。

如果开关可见但呈灰色且不活动,则需要将其设置为启用。

于 2010-01-19T20:04:25.083 回答