简体   繁体   English

TouchUpInside事件后无法将UIButton保持在选定状态

[英]Unable to keep UIButton in selected state after TouchUpInside Event

I have the need for an UIButton to maintain a pressed state. 我需要一个UIButton来维持一个按下状态。 Basically, if a button is in a normal state, I want to touch the button, it highlight to its standard blue color and then stay blue after lifting my finger. 基本上,如果按钮处于正常状态,我想触摸按钮,它突出显示其标准蓝色,然后抬起我的手指后保持蓝色。 I made the following UIAction and connected the buttons Touch Up Inside event to it. 我做了以下UIAction并将按钮Touch Up Inside事件连接到它。

-(IBAction) emergencyButtonPress:(id) sender
{
    if(emergencyButton.highlighted)
    {
        emergencyButton.selected = NO;
        emergencyButton.highlighted = NO;
    }
    else
    {
        emergencyButton.selected = YES;
        emergencyButton.highlighted = YES;
    }
}

But what happens, after I remove my finger, the button goes back to a white background. 但是,当我移开手指后,按钮会返回白色背景。 For a test I added a UISwitch and have it execute the same code: 对于测试,我添加了一个UISwitch并让它执行相同的代码:

-(IBAction) emergencySwitchClick:(id) sender
{
    if(emergencyButton.highlighted)
    {
        emergencyButton.selected = NO;
        emergencyButton.highlighted = NO;
    }
    else
    {
        emergencyButton.selected = YES;
        emergencyButton.highlighted = YES;
    }
}

In this case the button toggles to a highlighted and non-highlighted state as I would expect. 在这种情况下,按钮会切换到突出显示的非突出显示状态,如我所料。

Is there another event happening after the Touch Up Inside event that is resetting the state back to 'normal'? 在Touch Up Inside事件之后是否还有另一个事件正在将状态重置为“正常”? How do I maintain the highlighted state? 如何保持突出显示的状态?

The highlighted state is applied and removed by iOS when you touch / release the button. 当您触摸/释放按钮时,iOS会应用和删除突出显示的状态。 So don't depend on it in your action method. 所以不要在你的行动方法中依赖它。

As one of the other answers says, set the highlighted image to be the same as the selected image, and then modify your action method: 正如其他答案之一所说,将突出显示的图像设置为与所选图像相同,然后修改您的操作方法:

-(IBAction) emergencyButtonPress:(id) sender 
{
    emergencyButton.selected = !emergencyButton.selected;     
} 

If you want something like a toggle button, customize your "selected" state to be your "pressed" state, and then write something like this: 如果你想要一个类似切换按钮的东西,将你的“选中”状态自定义为你的“按下”状态,然后写下这样的东西:

- (IBAction)buttonTapped:(id)sender {
    [(UIButton*)sender setSelected:![sender isSelected]];
}

I had this same problem, and this is what worked for me: 我有同样的问题,这对我有用:

-(IBAction)buttonPressed:(id)sender {
if (isSelected) {
    [_button setSelected:NO];
    [_button setImage:[UIImage imageNamed:@"not_selected.png"] forState:UIControlStateSelected]; 
    isSelected=NO;

}
else {
    [_button setSelected:YES];
    [_button setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
    isSelected=YES;
}
}

if you want the above code to work, you should set an image for the state selected and one (even the same) for he state highlighted. 如果你想让上面的代码工作,你应该为所选状态设置一个图像,并为其状态突出显示一个(甚至相同)。

[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateHighlighted];
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateSelected];

hope it helps. 希望能帮助到你。

This worked for me: 这对我有用:

- (void)tapButton:(id)sender{
    UIButton *button = sender;
    if (!button.selected){
        [self performSelector:@selector(highlight:) withObject:button afterDelay:0.0];
    }else{
        [self performSelector:@selector(removeHighlight:) withObject:button afterDelay:0.0];
    }
}

- (void)highlight:(UIButton *)button{
    button.selected = !button.selected;
    button.highlighted = YES;
}

- (void)removeHighlight:(UIButton *)button{
    button.selected = !button.selected;
    button.highlighted = NO;
}

Try this simple answer.... 试试这个简单的答案....

- (void)mybutton:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = ![button isSelected]; // Important line
    if (button.selected)
    {
        NSLog(@"Selected");
        NSLog(@"%i",button.tag);
    }
    else
    {
        NSLog(@"Un Selected");
        NSLog(@"%i",button.tag);

    }
 }

This is not possible unfortunately, as soon as you let go Apple changes the state again. 不幸的是,这是不可能的,只要你放手苹果再次改变状态。 One option I've used before (but not pretty) is that you use a performSelector with delay 0.1 after the button is pressed to put it again in the selected state. 我之前使用的一个选项(但不是很漂亮)是在按下按钮后再使用延迟为0.1的performSelector将其再次置于选定状态。 This did work in my case. 这确实适用于我的情况。

EDIT: If you don't want to see the blinking effect, just set the delay to 0.0 编辑:如果您不想看到闪烁效果,只需将延迟设置为0.0

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

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