简体   繁体   English

UIButton在按下时保持突出显示

[英]UIButton staying highlighted when pressed

This is quite frustrating and I searched a LOT to no avail. 这非常令人沮丧,我搜索了很多都无济于事。

I have one button. 我有一个按钮。 When it is pressed it calls a method that performs a network action (NSURLRequest). 按下它时,它会调用执行网络操作的方法(NSURLRequest)。

The button should do the following: 该按钮应执行以下操作:

  • When pressed: Show the pressed state image, fire the request method 按下时:显示按下的状态图像,触发请求方法
  • After pressed: Show the disabled state of the button until method completes request 按下后:显示按钮的禁用状态,直到方法完成请求

The problem is the button is STAYING in the highlighted/pressed state throughout the request. 问题是在整个请求过程中按钮处于突出显示/按下状态的STAYING状态。 I have attached code I currently have below. 我附上了我目前在下面的代码。

For the Button: 对于按钮:

[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];

In the method at the start of the request: 在请求开始时的方法中:

-(void)reqMethod {
NSLog(@"Starting request..");
[myButton setHighlighted:NO];
[myButton setEnabled:NO];
[myButton setSelected:YES];

When the request completes it hides the normal button and shows a reset button which works fine. 请求完成后,它会隐藏正常按钮并显示一个正常工作的重置按钮。

You should have a view at multithreading documentation. 您应该有多线程文档的视图。 http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html

If you want a piece of code I think that this could be useful 如果你想要一段代码,我认为这可能很有用

[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];  
                     });
                });

Why dont you do network operation on background thread : 为什么不在后台线程上进行网络操作:

- (IBAction)buttonPressed:(UIButton *)button 
{

    [NSThread detachNewThreadSelector:@selector(doSomeNetworkStuff) toTarget:self    withObject:nil];
}

This will keep the button highlighted when you press it for the first time (you can alter it to work with images). 当您第一次按下按钮时,这将保持按钮突出显示(您可以将其更改为使用图像)。 If you press it again, it will become unhighlighted. 如果再次按下它,它将变得无法突出显示。 (I know the syntax looks weird, just try it...it works) (我知道语法看起来很怪异,试试吧......它有效)

@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
        }
    }];
}

Now, just call this method again when your request is finished. 现在,只需在请求完成后再次调用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM