xcode 保持一个 UIButton 按下(状态选择/突出显示),直到按下另一个按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11181340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:27:04  来源:igfitidea点击:

keeping a UIButton pressed(state selected/highlighted),until another button is pressed?

xcodeios5uibuttonuitabbarcontroller

提问by OccamsRazor

I have 3 buttons at the bottom of my view controller, btn1 btn2 btn3, I'm using them instead of the tab bar as it is not possible to entirely customize a tab bar as per my requirements.

我的视图控制器底部有 3 个按钮 btn1 btn2 btn3,我使用它们而不是标签栏,因为不可能根据我的要求完全自定义标签栏。

Now the issue is, when btn1 is pressed, I want it to change its image, to a grey rectangle, instead of the normal state image. I have set the images for both states in the outlet I declared for the button, btn1Outlet, using the setimage and uicontrolstate properties of the outlet.

现在的问题是,当按下 btn1 时,我希望它将其图像更改为灰色矩形,而不是正常状态图像。我已经使用插座的 setimage 和 uicontrolstate 属性在为按钮 btn1Outlet 声明的插座中设置了两种状态的图像。

The problem is, I can't keep the button selected until say either btn2 or btn3 is pressed. btn one changes to the selected state image only as long as it is pressed, the moment I leave it, it changes back to its normal state.

问题是,在按下 btn2 或 btn3 之前,我无法保持选择按钮。btn 只在按下时才会更改选定状态图像,一旦我离开它,它就会变回正常状态。

How can I keep btn1's image as selected image until any of the other 2 buttons are pressed?

在按下其他 2 个按钮中的任何一个之前,如何将 btn1 的图像保留为选定图像?

采纳答案by Pochi

What you did is set an image for the "Highlighted" state, this is why when you push it you can see your image.

您所做的是为“突出显示”状态设置图像,这就是为什么当您按下它时您可以看到您的图像。

What you want to do is

你想做的是

1) set the image for the SELECTED state

1) 将图像设置为 SELECTED 状态

2) create a property for your view controller (just controll drag the button to the header) using the assistant view (while on storyboard, second square on the top right)

2)使用助手视图(在故事板上,右上角的第二个方块)为您的视图控制器创建一个属性(只需控制将按钮拖动到标题)

3) on your method for the button's action type:

3)关于按钮操作类型的方法:

button.selected = !button.selected;

(obviously replace button to whatever you named your property to)

(显然,将按钮替换为您为属性命名的任何内容)

回答by Emma Li

Here's what I did:

这是我所做的:

  1. Link all 3 buttons to the following action method
  2. Create array of all 3 buttons
  3. Set the button that invoked the method to selected
  4. Set the other 2 buttons to not selected

    - (IBAction)buttonPressed:(id)sender
    {
        NSArray* buttons = [NSArray arrayWithObjects:btn1, btn2, btn3, nil];
        for (UIButton* button in buttons) {
            if (button == sender) {
                button.selected = YES;
            }
            else {
                button.selected = NO;
            }
        }
    }
    
  1. 将所有 3 个按钮链接到以下操作方法
  2. 创建所有 3 个按钮的数组
  3. 将调用该方法的按钮设置为选中
  4. 将其他 2 个按钮设置为未选中

    - (IBAction)buttonPressed:(id)sender
    {
        NSArray* buttons = [NSArray arrayWithObjects:btn1, btn2, btn3, nil];
        for (UIButton* button in buttons) {
            if (button == sender) {
                button.selected = YES;
            }
            else {
                button.selected = NO;
            }
        }
    }
    

Hope this helps.

希望这可以帮助。

Cheers!

干杯!

回答by Kpmurphy91

To keep the button selected, you need to call setSelected:YES in the method that your button calls. For example:

要保持按钮被选中,您需要在按钮调用的方法中调用 setSelected:YES。例如:

- (void) methodThatYourButtonCalls: (id) sender {
        [self performSelector:@selector(flipButton:) withObject:sender afterDelay:0.0];


}

- (void) flipButton:(UIButton*) button {
    if(button.selected) 
        [button setSelected:NO];
    else
        [button setSelected:YES];

}

I know it looks a little weird calling performSelector: instead of just calling [sender setSelected:YES], but the latter never works for me, while the former does!

我知道调用 performSelector: 而不是仅仅调用 [sender setSelected:YES] 看起来有点奇怪,但后者对我不起作用,而前者起作用!

In order to get the buttons to deselect when a different one is pressed, I suggest adding an instance variable that holds a pointer to the currently selected button, so when a new one is touched you can call flipButton: to deselect the old one accordingly. So now your code should read:

为了在按下不同按钮时取消选择按钮,我建议添加一个实例变量,该变量包含指向当前选定按钮的指针,因此当触摸新按钮时,您可以调用flipButton: 相应地取消选择旧按钮。所以现在你的代码应该是:

add a pointer to your interface

添加指向您的界面的指针

@interface YourViewController : UIViewController
{
    UIButton *currentlySelectedButton;
}

and these methods to your implementation

并将这些方法用于您的实现

- (void) methodThatYourButtonCalls: (id) sender {
    UIButton *touchedButton = (UIButton*) sender;

    //select the touched button 
    [self performSelector:@selector(flipButton:) withObject:sender afterDelay:0.0]; 

    if(currentlySelectedButton != nil) { //check to see if a button is selected...
        [self flipButton:currentlySelectedButton];

    currentlySelectedButton = touchedButton;
}

- (void) flipButton:(UIButton*) button {
    if(button.selected) 
        [button setSelected:NO];
    else
        [button setSelected:YES];

}