开发者

Keep iPhone UIButton Highlighted

开发者 https://www.devze.com 2022-12-21 01:22 出处:网络
I have the following code snippets: @interface Foo: UIViewController { ... UIButton *myButton; ... } @implementation Foo

I have the following code snippets:

@interface Foo: UIViewController {
  ...
  UIButton *myButton;
  ...
}

@implementation Foo

- (void) viewDidLoad {
  ...
  myButton.highlighted = YES;
  ...
}

When I run the app, the button is highlighted in blue (default behavior). It works as I expected.

But after pressing the button once, the button is no longer highlighted.

Then, I created an IBAction highlightButton to handle Touch Up Insi开发者_开发百科de event where I explicitly call myButton.highlighted = Yes;. Unfortunately, the button highlight still does not stay.

How can I keep it highlighted in blue even after being pressed?


The solution is to do [button setHighlighted:YES] in the next runloop:

- (void)highlightButton:(UIButton *)b { 
    [b setHighlighted:YES];
}

 - (IBAction)onTouchup:(UIButton *)sender {
    [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
}


The simplest code is here.

dispatch_async(dispatch_get_main_queue(), ^{
    [button setHighlighted:YES];
});


An alternate way to run this is by sending a block to the main operation queue:

-(void)onTouchup:(UIButton*) button
{
    [NSOperationQueue.mainQueue addOperationWithBlock:^{ button.highlighted = YES; }];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号