菜鸟教程小白 发表于 2022-12-12 21:20:20

objective-c - UIButtons 的 IBOutletCollection - 更改按钮的选定状态


                                            <p><p>我在一个 View 中遇到了多个 UIButtons 的问题。我希望单独选择按钮,一次选择多个(例如:10 个按钮,选择按钮 1、4、5、9)。 </p>

<p>在我的标题中,我有一个 IBOutletCollection 的属性:</p>

<pre><code>@property (retain, nonatomic) IBOutletCollection(UIButton) NSMutableArray *buttonToStaySelected;
</code></pre>

<p>在我的实现中,我有一个 IBAction:</p>

<pre><code>-(IBAction)selectedButton:(id)sender{
for (UIButton *b in self.buttonToStaySelected) {
   if (b.isSelected == 0){
      ;
} else
      ;
}
}
</code></pre>

<p>我遇到的问题是,当我选择与集合相关的任何按钮时,它们都会变为选中状态。我知道问题很可能(几乎可以肯定)出在循环中,但我试图规定的每个条件都会破坏代码,并且没有一个按钮能够“改变”状态。</p>

<p><em><strong>更新</strong></em></p>

<p>为了让它们可选择、更改状态并勾选多个,我将其用作我的最终代码:</p>

<pre><code>-(IBAction)selectedButton:(id)sender {
for (UIButton *b in self.buttonToStaySelected) {
      if (sender == b) {
      ;
    }
   }
}
</code></pre>

<p>感谢大家的帮助!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>selectButton:</code> 消息带有指定被点击按钮的参数发送,但您将操作应用于集合中的所有按钮,而不仅仅是被点击的按钮。</p >

<pre><code>-(IBAction)selectedButton:(id)sender
{
for (UIButton *b in self.buttonToStaySelected)
{
   if (sender == b)
   {
      b.isSelected == !b.isSelected
   }
}
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - UIButtons 的 IBOutletCollection - 更改按钮的选定状态,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10888422/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10888422/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - UIButtons 的 IBOutletCollection - 更改按钮的选定状态