Cocoa-Touch UIButton isSelected 说明

发布于 2024-09-08 13:55:25 字数 608 浏览 4 评论 0原文

我对 Cocoa 编程相当陌生,但我一直在非常努力地学习这门语言,直到我遇到了这个障碍,我似乎无法绕过/破解我的方法,(并不是我想要的) 。我更愿意做对!)

在我看来,在IB中我有一个带有按钮的工具栏,我想做的是模仿地图应用程序。我希望能够按下该按钮,然后弹出我的位置,同时保持该按钮处于选中状态,然后当再次按下该按钮时,取消选择它,从而从地图上删除蓝色斑点位置。

理想情况下,我想使用以下代码,但 if 语句似乎不想在模拟器上工作(我认为如果我在 iPhone 上尝试,它不会改变。)

-(IBAction) showLocation: (id) sender
{
 if([sender isSelected]) // this doesn't work!!
 {
  [sender setSelected:NO];
  mapView.showsUserLocation = FALSE;
 }
 else
 {
  [sender setSelected:YES];
  mapView.showsUserLocation = TRUE; 
 }
}

显然,如果我摆脱 if声明,我知道我可以显示位置并根据需要设置所选属性,但我似乎无法从按钮“获取”所选属性...执行此操作的正确方法是什么?

I'm rather new to programming in Cocoa, but I've been working on learning the language quite diligently up until I hit this snag that I can't seem to circumvent/hack my way around, (not that I'd want to. I'd prefer to do it right!)

Where I stand, In IB I have a toolbar that has a button and what I'm trying to do is mimic the maps app. I want to be able to press the button and then have my location pop up, while keeping the button selected, then when it's pressed again, deselect it and thus remove the blue blip location from the map.

ideally, I would like to use the following code, but the if statement doesn't seem to want to work on the simulator (which I presume wouldn't change if I tried on the iPhone.)

-(IBAction) showLocation: (id) sender
{
 if([sender isSelected]) // this doesn't work!!
 {
  [sender setSelected:NO];
  mapView.showsUserLocation = FALSE;
 }
 else
 {
  [sender setSelected:YES];
  mapView.showsUserLocation = TRUE; 
 }
}

obviously if I get rid of the if statement, I know that I can show the location and set the selected as I liked, but I can't seem to "get" the selected property from the button... what's the right way of doing this?

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

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

发布评论

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

评论(1

怪我闹别瞎闹 2024-09-15 13:55:25

尝试

- (void)methodName:(UIButton *)sender
{
if (sender.selected == YES) ...

try

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