0

我有一些关于 uibutton 的问题。我想要一个按钮在按下时突出显示,但在按下其他按钮时取消突出显示,但我无法让上一个按钮取消突出显示。有人可以请教吗?

@interface ViewController : UIViewController
{
    UIButton *button1;
    UIButton *button2;
}
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;

-(IBAction)button1:(id)sender;
-(IBAction)button2:(id)sender;
-(void) highlightButton: (UIButton *) button;
-(void) unhighlightButton: (UIButton *) button;
@end


@interface ViewController ()
@end
@implementation ViewController
@synthesize button1, button2;

-(IBAction)button1:(id)sender
{

    [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
    [button2 performSelector:@selector(unhighlightButton:) withObject:button2 afterDelay:0.0];
}

-(IBAction)button2:(id)sender
{
    [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
    [button1 performSelector:@selector(unhighlightButton:) withObject:button1 afterDelay:0.0];
}

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

-(void) unhighlightButton:(UIButton *)button
{
    [button setHighlighted:NO];
}

@end
4

4 回答 4

1

首先检查IBOutletIBAction连接是否正确连接。

-(IBAction)button1:(id)sender
    {
        [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
        [self performSelector:@selector(unhighlightButton:) withObject:button2 afterDelay:0.0];
    }

    -(IBAction)button2:(id)sender
    {
        [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
        [self performSelector:@selector(unhighlightButton:) withObject:button1 afterDelay:0.0];
    }

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

    -(void) unhighlightButton:(UIButton *)button
    {
        [button setHighlighted:NO];
    }
于 2013-08-17T09:00:14.867 回答
1

由于您使用的是属性,请以两种方式更改这两行:

[self performSelector:@selector(unhighlightButton:) withObject:self.button2 afterDelay:0.0];

[self performSelector:@selector(unhighlightButton:) withObject:self.button1 afterDelay:0.0];

1) 属性应以“ self.”表示

2)您正在调用unhighlightButton:位于视图控制器中的方法“”,因此您需要在“ self”而不是“ button”上调用它。“ button”通过“ withObject:”参数传递。

performSelector无论如何,您为什么要使用“ ”方法?

您可以重写其中一种操作方法:

-(IBAction)buttonTouched:(id)sender
{
    [sender setHighlighted: YES];
    if(sender == self.button1)
    {
       [self.button2 setHighlighted: NO];
    } else {
       [self.button1 setHighlighted: NO];
    }
}

并将两个按钮都指向那个 IBAction。

于 2013-08-17T08:56:29.377 回答
0

您还可以使用 UIButton 的 selected 属性:对于两个按钮选择器将是相同的“buttonTouched”

-(IBAction)buttonTouched:(id)sender {

     button1.selected = NO;
     button2.selected = NO;

     if (sender == button1) {
          button1.selected = YES;
     } else if (sender == button2) {
          button2.selected = YES;
     }
}

尝试这个。

于 2013-08-17T09:40:49.473 回答
0

您可以执行以下操作...

 -(IBAction)button1:(id)sender
 {

 button1.highlighted = YES;
  button2.highlighted = NO;
 }

 -(IBAction)button2:(id)sender
 {
  button2.highlighted = YES;
  button1.highlighted = NO;
 }

让我知道它是否有效!!!!

快乐编码!!!!

于 2013-08-17T09:51:25.740 回答