触摸后保持选择UIButton [英] Keeping a UIButton selected after a touch

查看:98
本文介绍了触摸后保持选择UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的用户点击一个按钮后,我希望该按钮在我执行网络操作期间保持按下状态。网络操作完成后,我希望按钮返回默认状态。

After my user clicks a button, I'd like that button to stay pushed during the time that I perform a network operation. When the network operation is complete, I want the button to return to its default state.

我试过调用 - [UIButton setSelected:YES ] 在按下按钮后(在我的网络操作完成后相应调用 - [UIButton setSelected:NO] )但它没有好像什么都做。同样的事情,如果我打电话给 setHighlighted:

I've tried calling -[UIButton setSelected:YES] right after the button push (with a corresponding call to -[UIButton setSelected:NO] after my network op finishes) but it doesn't seem to do anything. Same thing if I call setHighlighted:.

我想我可以尝试换出背景图片来表示选中在网络操作期间的状态,但这似乎是一个黑客。还有更好的建议吗?

I suppose I could try swapping out the background image to denote a selected state for the duration of the network op, but that seems like a hack. Any better suggestions?

以下是我的代码:

- (IBAction)checkInButtonPushed
{
    self.checkInButton.enabled = NO;
    self.checkInButton.selected = YES;
    self.checkInButton.highlighted = YES;
    [self.checkInActivityIndicatorView startAnimating];
    [CheckInOperation startWithPlace:self.place delegate:self];
}

- (void)checkInCompletedWithNewFeedItem:(FeedItem*)newFeedItem wasNewPlace:(BOOL)newPlace possibleError:(NSError*)error;
{
    [self.checkInActivityIndicatorView stopAnimating];
    self.checkInButton.enabled = YES;
    self.checkInButton.selected = NO;
    self.checkInButton.highlighted = NO;
}


推荐答案

你是如何设置图像的对于按钮上的不同 UIControlStates ?您是为 UIControlStateHighlighted 设置背景图像以及 UIControlStateSelected

How are you setting the images for the different UIControlStates on the button? Are you setting a background image for UIControlStateHighlighted as well as UIControlStateSelected?

UIImage *someImage = [UIImage imageNamed:@"SomeResource.png"];
[button setBackgroundImage:someImage forState:UIControlStateHighlighted];
[button setBackgroundImage:someImage forState:UIControlStateSelected];

如果您在按钮上设置选定状态,请触摸事件而不是在内部触摸,按钮实际上处于突出显示+选中状态,所以你也想设置它。

If you're setting the selected state on the button touch down event rather than touch up inside, your button will actually be in a highlighted+selected state, so you'll want to set that too.

[button setBackgroundImage:someImage forState:(UIControlStateHighlighted|UIControlStateSelected)];



编辑:



总结我的评论中的评论和解决您发布的代码...您需要为您所在的完整 UIControl 状态设置背景图片。根据您的代码段,在网络操作期间,此控制状态将被禁用+选中+突出显示。这意味着你需要这样做:

To sum up my remarks in the comments and to address the code you posted...you need to set your background images for the full UIControl state that you're in. According to your code snippet, this control state would be disabled + selected + highlighted for the duration of the network operation. This means that you would need to do this:

[button setBackgroundImage:someImage forState:(UIControlStateDisabled|UIControlStateHighlighted|UIControlStateSelected)];

如果你删除 highlight = YES ,然后你需要这个:

If you remove the highlighted = YES, then you would need this:

[button setBackgroundImage:someImage forState:(UIControlStateDisabled|UIControlStateSelected)];

获取图片?

这篇关于触摸后保持选择UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆