@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状态(蓝色)。
答案 0 :(得分:0)
初始化时或者值从数据更改时,您需要为批准值的开关调用– setOn:animated:
。
如果交换机可见但是显示为灰色且处于非活动状态,则需要将其设置为启用。