2012-10-17 54 views
3

我目前有一個UISwitch,當打開和關閉分別遞增和遞減計數器。UISwitch雙擊

當計數器爲0時,計數器不會遞減。從功能上來說,這個工作非常完美,但是我注意到了一個bug,並想知道是否有人經歷過這個。

本質上,如果您非常快速地在其遠端位置雙擊UISwitch(完全打開或關閉),則計數器將遞增兩次,因爲我想像的是UISwitch未完全打開關閉狀態,因此只是添加到櫃檯再次沒有先遞減。

下面是我用做交換機上的檢查代碼:

// Sliders modified 

- (IBAction)personalityChanged:(id)sender { 
    if ([personality isOn]){ 
     [[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"1"]; 
     rating ++; 
     NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]); 
     [personality set] 
    } 
    else { 
     [[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"0"]; 
     [self subtractFromRating:nil]; 
     NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]); 
    } 
    [self checkRating:nil]; 
} 

然後減法的評價:

// subtract from rating 

-(void)subtractFromRating:(id)sender{ 
    if (rating == 0) { 
     // do nothing 
    } 
    else 
    { 
     rating --; 
    } 
} 

而且,如果滑塊是發生了什麼最後的結果一個位置:

// check rating 

-(void)checkRating:(id)sender{ 
    switch (rating) { 
     case 0: 
      [matchRating setText:@""]; 
      [ratingGraphic setImage:[UIImage imageNamed:@""]]; 
      NSLog(@"rating is 0"); 
      break; 
     case 1: 
      [matchRating setText:@"Single Match"]; 
      [ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic1.png"]]; 
      NSLog(@"rating is 1"); 
      break; 
     case 2: 
      [matchRating setText:@"Potential Match"]; 
      [ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic2.png"]]; 
      NSLog(@"rating is 2"); 
      break; 
     case 3: 
      [matchRating setText:@"Great Match"]; 
      [ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic3.png"]]; 
      NSLog(@"rating is 3"); 
      break; 
     case 4: 
      [matchRating setText:@"Hot Match"]; 
      [ratingGraphic setImage:[UIImage imageNamed:@"ratinggraphic4.png"]]; 
      NSLog(@"rating is 4"); 
      break; 
     default: 
      break; 
    } 
} 

是否有辦法確保開關從開啓狀態變爲關閉fu在返回之前還是更好的方法呢?

回答

3

檢測實際上是否發生變化的解決方案是保留追蹤最後一個開關狀態的附加BOOL變量。

BOOL lastValue = NO; // initial switch state 
- (IBAction)personalityChanged:(id)sender { 
    if (personality.isOn != lastValue) { 
     lastValue = personality.isOn; 
     if ([personality isOn]){ 
      [[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"1"]; 
      rating ++; 
      NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]); 
      [personality set] 
     } 
     else { 
      [[[GlobalData sharedGlobalData]personalitySliderValue] replaceObjectAtIndex:currentRecord-1 withObject:@"0"]; 
      [self subtractFromRating:nil]; 
      NSLog(@"The value of personality slider is %@", [[[GlobalData sharedGlobalData]personalitySliderValue] objectAtIndex:currentRecord-1]); 
     } 
     [self checkRating:nil]; 
    } 
} 

這隻會在開關狀態實際發生改變時纔會執行。

+0

謝謝wsidell,像夢一樣工作,現在滑塊的行爲就像他們應該做的一樣.. wohoo! – SmokersCough