2014-06-25 30 views
0

嗨,大家好,我正在嘗試創建一個包含7個不同部分的滑塊,因爲您滑動滑塊時標籤會發生變化。iOS的編碼滑塊的問題

我已經將它與所有.m .h鏈接,所以標籤與滑塊等文件相互作用,但只有值爲0,0.5和1的那些似乎正在工作?

有人能指出我做錯了什麼的正確方向嗎? Im新的iOS開發,這是我第一次使用滑塊。

這是我在.m文件代碼滑塊

-(IBAction)slidetheslider:(id)sender; { 
q4label1.text = [NSString stringWithFormat:@"%0.1f",question4slider.value]; 
if (question4slider.value == 0) { 
    q4label2.text = @"> 2.5 dots L"; 
         } 
if (question4slider.value == 0.2) { 
    q4label2.text = @" 2.5 dots L"; 
} 
if (question4slider.value == 0.3) { 
    q4label2.text = @"1-2 dots L"; 
} 
if (question4slider.value == 0.5) { 
    q4label2.text = @"< 1 dot L/R"; 
} 
if (question4slider.value == 0.7) { 
    q4label2.text = @"1-2 dots R"; 
} 
if (question4slider.value == 0.8) { 
    q4label2.text = @"2.5 dots R"; 
} 
if (question4slider.value == 1) { 
    q4label2.text = @"> 2.5dots R"; 
} 

}

+0

也許這個[tutorial](http://www.techrepublic.com/blog/software-engineer/better-code-uislider-basics-for-apple-ios/)會幫助你。 – snowdragon

回答

0

question4slider.value是一個浮點數。由於它存儲的方式,它不會完全在內存中表示,有時候0.2被存儲爲0.01999999999。所以不要使用絕對比較。試試這個:

-(IBAction)slidetheslider:(id)sender; { 
q4label1.text = [NSString stringWithFormat:@"%0.1f",question4slider.value]; 
if (question4slider.value < 0.2) { 
    q4label2.text = @"> 2.5 dots L"; 
         } 
} else if (question4slider.value < 0.3) { 
    q4label2.text = @" 2.5 dots L"; 
} 
else if (question4slider.value < 0.5) { 
    q4label2.text = @"1-2 dots L"; 
} 
else if (question4slider.value < 0.7) { 
    q4label2.text = @"< 1 dot L/R"; 
} 
else if (question4slider.value < 0.8) { 
    q4label2.text = @"1-2 dots R"; 
} 
else if (question4slider.value < 1.0) { 
    q4label2.text = @"2.5 dots R"; 
} 
else 
    q4label2.text = @"> 2.5dots R"; 
} 
+0

嗨歐文謝謝你的答覆,我已經調整了代碼,但仍然有問題,它提出了一條錯誤消息的線路。 預期的標識符或'('? – user3607744

+0

編輯:不要擔心所有排序!謝謝你的幫助! – user3607744

+0

如果這回答你的問題,請將其標記爲已回答,以便其他人不會繼續調查它。 –