ios 按下时如何获取 UIButton 的标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14199256/
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-08-30 21:44:56  来源:igfitidea点击:

How to get the title for a UIButton when it is pressed

iphoneobjective-ciosuibutton

提问by hanumanDev

I'm trying to discover what the UIButton title is for which UIButton was pressed in the following code.

我试图找出在以下代码中按下 UIButton 的 UIButton 标题是什么。

on viewDidLoad the button title is outputted to the console using:

在 viewDidLoad 按钮标题输出到控制台使用:

        NSLog(@"The button title is %@ ", btn.titleLabel.text);

I would like to get this title when a button is pressed instead.

我想在按下按钮时获得此标题。

thanks for any help

谢谢你的帮助

:)

:)

// Create buttons for the sliding category menu.
        NSMutableArray* buttonArray = [NSMutableArray array];
        NSArray * myImages = [NSArray arrayWithObjects:@"category-cafe-unsel.png", @"category-food-unsel.png", @"category-clothing-unsel.png", @"category-health-unsel.png", @"category-tech-unsel_phone.png" , @"category-tech2-unsel.png", @"catefory-theatre-unsel.png", @"category-travel-unsel.png", nil];

        // only create the amount of buttons based on the image array count
        for(int i = 0;i < [myImages count]; i++)
        {
            // Custom UIButton

            btn = [UIButton buttonWithType:UIButtonTypeCustom];

            [btn setFrame:CGRectMake(0.0f, 20.0f, 52.0f, 52.0f)];
            [btn setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateNormal];

            NSLog(@"The button title is %@ ", btn.titleLabel.text);

            [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [buttonArray addObject:btn];

            NSLog(@"Button tag is: %d",btn.tag);

        }

回答by Zaphod

In your action:

在你的行动中:

- (void) buttonPressed:(UIButton*) sender {
    NSLog(@"The button title is %@",sender.titleLabel.text);
}

Edit: Or as commented Iulian: sender.currentTitle, but it may be nil, see the comments of Michael.

编辑:或如 Iulian: 评论的那样sender.currentTitle,但可能是nil,请参阅迈克尔的评论。

回答by Fogmeister

You should have the function somewhere...

你应该在某个地方有这个功能......

- (void)buttonPressed:(id)sender

Just put this inside it...

把这个放进去就行了...

- (void)buttonPressed:(id)sender
{
    UIButton *someButton = (UIButton*)sender;

    NSLog(@"The button title is %@ ", [someButton titleForState:UIControlStateNormal]);

    //You should also be able to use...
    //NSLog(@"The button title is %@ ", someButton.titleLabel.text);
}

回答by Fogmeister

NSString * strButtonTitle = [btnButton titleForState:state];

where state:

其中状态:

UIControlStateNormal,
UIControlStateHighlighted,
UIControlStateDisabled,
UIControlStateSelected,
UIControlStateFocused,
UIControlStateApplication,
UIControlStateReserved

For your requirement

满足您的要求

NSString * strButtonTitle = [btnButton titleForState:UIControlStateSelected];

This is the method for getting the title for different states of button at any given time in the program...But based on the question answer from Zaphodis good.

这是在程序中的任何给定时间获取按钮不同状态的标题的方法......但基于Zaphod的问题答案是好的。