更改按钮单击上的不同按钮的按钮文本

时间:2016-12-29 00:34:14

标签: ios objective-c

我的storyboard和ViewController中有3个按钮按预期工作:

- (IBAction)button0:(id)sender {
    [sender setTitle:@"btn0 pressed" forState:UIControlStateNormal];
}
- (IBAction)button1:(id)sender {
    [sender setTitle:@"btn1 pressed" forState:UIControlStateNormal];
}
- (IBAction)button2:(id)sender {
    [sender setTitle:@"btn2 pressed" forState:UIControlStateNormal];
}

我有第四个按钮,当按下时,我想将button0-2的显示文本更改为空字符串。

- (IBAction)resetAllButtons:(id)sender {
    //In Android this code would be something like:
    //Button btn0 = (Button) findViewById(R.id.button0);
    //btn0.setText(" ");
}

我该怎么做?我找到了许多方法来更改按钮文本,但只有当前按钮被按下。我可以用ID以某种方式定位所有按钮吗?

1 个答案:

答案 0 :(得分:0)

想出来(虽然还不清楚它为何起作用?)

我在我的ViewController.h文件中将button0连接为IBOutlet。

<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .section {
            position: relative;
            width: 200px;
            height: 200px;
        }
        
        .changeimg {
            position: absolute;
            display: none;
            width: 200px;
            height: 200px;
        }
        
        .show {
            display: block;
        }
        
        .changebutton {
            width: 100px;
            position: relative;
            color: blue;
        }
    </style>
</head>

<body>
    <div class="section">
        <img class="changeimg show" id="first" style="background-color:aqua;" src="images/cresta.svg">
        <img class="changeimg" style="background-color:red  " src="images/longitud.svg">
        <img class="changeimg" style="background-color:black " src="images/amplitud.svg">
        <img class="changeimg" style="background-color:wheat " src="images/frecuencia.gif">
    </div>
    <div class="changebutton">Cambiar</div>
</body>
<script>
    $('.changebutton').click(function() {
        var show = $('.show');
        show.removeClass('show');
        if (show.next().length > 0) {
            show.next().addClass('show');
        } else {
            $('#first').addClass('show');
        }
    });
</script>

</html>

从那里我可以使用

在我的ViewController.m文件中引用它
@property (weak, nonatomic) IBOutlet UIButton *button0;

但为什么我能做到这一点?我想如果我宣布

[self.button0 setTitle:@" " forState:UIControlStateNormal];

在我的ViewControler.h文件中,我也没有连接到同一对象的插座?感谢您阅读任何一种方式。