2013-08-07 5 views
1
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 

    if (self) { 
     self.sw = [[[UISwitch alloc] initWithFrame:CGRectMake(210, 8, 50, 30)] autorelease]; 
     [sw setOn:YES]; 
     [sw addTarget:self action:@selector(switchChanged) forControlEvents:UIControlEventTouchUpInside]; 

     [self addSubview:sw]; 
    } 

    return self; 
} 

-(void)switchChanged 
{ 
    // [self setImage]; 
    [sw setOn:NO]; 
    if (_delegate && [_delegate respondsToSelector:@selector(selectedWith:)]){ 
     [_delegate selectedWith:self]; 
    } 
} 

lorsque je clique sur le commutateur, mais la fonction switchChanged ne pas être appelée?pourquoi je clique sur le UISwitch, ça ne marche pas?

+0

Non pertinent par rapport à votre question, mais vous ne devez pas modifier la propriété 'isOn' de' UISwitch' dans 'switchChanged'. Il va le gérer automatiquement. – Desdenova

+0

UIControlEventTouchUpInside changer cela comme réponses ci-dessous.Basiquement fonctionne dans le bouton UI. – Subrat

Répondre

1

changement forControlEvents à UIControlEventValueChanged au lieu UIControlEventTouchUpInside

Essayez ceci,

[sw addTarget:self action:@selector(switchChanged) forControlEvents:UIControlEventValueChanged]; 
0

Parce que l'événement spécifié doit être UIControlEventValueChanged lorsque vous avez besoin d'une action sur le changement de valeur dans UISwitch

[sw addTarget:self action:@selector(switchChanged) forControlEvents: UIControlEventValueChanged]; 
+0

wow ... c'était vraiment utile ... –

Questions connexes