簡體   English   中英

UIButton發件人標題問題

[英]UIButton sender title problem

似乎是一個很簡單的問題,但我不明白。 它有兩個UIButton,一個名為“下一個”,另一個名為“上一個”。 兩者都鏈接到相同的方法。 我要做的就是根據按下的按鈕來更改變量“ helpStatus”:

    if([sender currentTitle] == @"next"){
        helpStatus++;
    } 
    if ([sender currentTitle] == @"previous"){
        helpStatus--;
    }

    NSLog(@"%@", [sender currentTitle]);

記錄的標題是“下一個”和“上一個”,就像應該的那樣,但是它不起作用,我也不知道為什么。

您需要使用isEqualToString,否則您只是在比較它們是否是同一對象,而不是相等:)

if([[sender currentTitle] isEqualToString:@"next"]){
    helpStatus++;
} 
if ([[sender currentTitle] isEqualToString:@"previous"]){
    helpStatus--;
}

我不會使用該字符串。 如果您決定更改標簽會怎樣? 考慮改用標簽。

例如b

 button.tag = 100;

    ...

- (void)buttonPressed:(id)sender {
    UIButton *button = (UIButton*)sender;

    if(button.tag == 100) {

    }
}

或您的情況(我在開玩笑),甚至:

button1.tag = 1;
button2.tag = -1;

        ...

- (void)buttonPressed:(id)sender {
   UIButton *button = (UIButton*)sender;
   helpStatus+= button.tag;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM