为什么这不起作用? if ( [mybutton isSelected] )

发布于 2024-12-24 22:23:13 字数 701 浏览 6 评论 0原文

我的 GUI 中有 32 个按钮...(不要问)...

无论如何,如果只选择(通过点击)其中四个按钮,那么我想要为那些选定的按钮做一些事情。

我的问题是,当我检查每个按钮的“selected”属性时,它们都返回 true。

我还使用界面生成器,在那里我指定了一个图标,每当按钮处于选定状态时就显示在按钮上。每个按钮还有一个范围从 100 到 132 的标签值。所选按钮的图标效果非常好!当您点击 4 个按钮时,这 4 个按钮就会有图标而没有其他按钮...但是,当我运行代码来检测选择了哪些按钮时,它们都返回 true。

有什么想法我做错了吗?

for (int j = 1; j <= 32; j++) {
        but = (UIButton *)[self.view viewWithTag:(j + 100)];
        if(but.selected == YES) {
            // Note: I have also tried if ([but isSelected])
            //       I have also tried if ([but isSelected] == YES)
            //
            // every button is entering this section of code...
            //
        }
}

I have 32 buttons in my GUI... (don't ask)...

Anyway, if only select (by tapping) four of the buttons then I'd like to something for those buttons that are selected.

My problem is that when I check each button's "selected" property, they all return true.

I am also using the interface builder and it is there that I have specified an icon to display on the button whenever it is in the selected state. Each button also has a tag value ranging from 100 to 132. The icon for selected buttons is working great! When you tap 4 buttons then those 4 buttons have the icon and no others... However, when I run the code to detect which are selected, all of them return true.

Any ideas what I am doing wrong?

for (int j = 1; j <= 32; j++) {
        but = (UIButton *)[self.view viewWithTag:(j + 100)];
        if(but.selected == YES) {
            // Note: I have also tried if ([but isSelected])
            //       I have also tried if ([but isSelected] == YES)
            //
            // every button is entering this section of code...
            //
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-12-31 22:23:13

检查 Button_clicked 方法的发送者,而不是检查 "but.selected" 。单击按钮时,它应该只是 32 个按钮中的一个。

替换

but.selected == YES

but == sender

我认为这肯定会解决你的问题。

Check the sender of button_clicked Method instead of checking "but.selected" . It should be only one from 32 buttons when you click a button.

replace

but.selected == YES

with

but == sender

I think this will solve your problem definitely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文