这非常令人沮丧,我搜索了很多都无济于事。
我有一个按钮。按下它时,它会调用执行网络操作的方法(NSURLRequest)。
按钮应执行以下操作:
问题是在整个请求过程中按钮处于突出显示/按下状态的STAYING状态。我附上了我目前在下面的代码。
对于按钮:
[myButton setBackgroundImage:[UIImage imageNamed:@"defaultbutton"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:@"pressedbutton"] forState:UIControlStateHighlighted];
[myButton setBackgroundImage:[UIImage imageNamed:@"disabledbutton"] forState:(UIControlStateDisabled|UIControlStateSelected)];
[squishButton addTarget:self action:@selector(reqMethod) forControlEvents:UIControlEventTouchUpInside];
在请求开始时的方法中:
-(void)reqMethod {
NSLog(@"Starting request..");
[myButton setHighlighted:NO];
[myButton setEnabled:NO];
[myButton setSelected:YES];
当请求完成时,它会隐藏正常按钮并显示一个正常工作的重置按钮。
答案 0 :(得分:2)
如果你想要一段代码,我认为这可能很有用
[myButton setBackgroundImage:[UIImage imageNamed:@"defaultbutton"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:@"pressedbutton"] forState:UIControlStateHighlighted];
[myButton setBackgroundImage:[UIImage imageNamed:@"disabledbutton"] forState:(UIControlStateDisabled|UIControlStateSelected)];
[squishButton addTarget:self action:@selector(reqMethod) forControlEvents:UIControlEventTouchUpInside];
dispatch_async(dispatch_get_global_queue(0, 0),
^{
//Your request
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"Starting request..");
[myButton setHighlighted:NO];
[myButton setEnabled:NO];
[myButton setSelected:YES];
});
});
答案 1 :(得分:1)
为什么不在后台线程上进行网络操作:
- (IBAction)buttonPressed:(UIButton *)button
{
[NSThread detachNewThreadSelector:@selector(doSomeNetworkStuff) toTarget:self withObject:nil];
}
答案 2 :(得分:0)
当您第一次按下按钮时,这将保持按钮突出显示(您可以将其更改为使用图像)。如果再次按下它,它将变得无法突出显示。 (我知道语法看起来很怪异,试试吧......它有效)
@property (nonatomic) BOOL buttonHighlighted
// IBAction called when button pressed the first time
- (IBAction)buttonPressed:(UIButton *)button
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if(self.buttonHighlighted)
{
button.highlighted = NO;
self.buttonHighlighted = NO;
}
else
{
button.highlighted = YES;
self.buttonHighlighted = YES;
//Fire request method
}
}];
}
现在,只需在请求完成后再次调用此方法。