在我的用户单击一个按钮后,我希望该按钮在我执行网络操作期间保持按下状态.当网络操作完成后,我希望按钮恢复到默认状态.
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)];
如果您删除 highlighted = YES
,那么您将需要这个:
If you remove the highlighted = YES
, then you would need this:
[button setBackgroundImage:someImage forState:(UIControlStateDisabled|UIControlStateSelected)];
获取图片?
这篇关于触摸后保持 UIButton 处于选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!