【问题标题】:How to get the title for a UIButton when it is pressed按下时如何获取 UIButton 的标题
【发布时间】:2013-01-07 15:47:12
【问题描述】:

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

在 viewDidLoad 上,按钮标题使用以下命令输出到控制台:

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

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

感谢您的帮助

:)

// 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);

        }

【问题讨论】:

    标签: iphone objective-c ios uibutton


    【解决方案1】:

    在你的行动中:

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

    编辑: 或者如 Iulian 所评论:sender.currentTitle,但也可能是 nil,请参阅 Michael 的 cmets。

    【讨论】:

    • 这和sender.currentTitle有什么区别?
    • 我没有说不是,我只是在问。刚才currentTitleniltitleLabel.text 不是。
    • 这让我开始思考,我检查了文档。我更喜欢的第一个答案是获取标签对象(这就是我喜欢它的原因),然后是文本属性。第二个,currentTitle,是特定于状态的,可能为 nil。从文档 r.e. currentTitle:每当按钮状态更改时,都会自动设置此属性的值。对于没有与之关联的自定义标题字符串的状态,此方法返回当前显示的标题,通常是与 UIControlStateNormal 状态关联的标题。该值可能为零。
    • @MichaelOlenick,我在发表评论后也读到了,但仍然很奇怪,如果titleLabel.text 不为零,这意味着该按钮有一个文本集(也是我的情况),那么currentTitle 不应该返回相同的文本吗?
    • 是的-在您的示例中似乎不应该为零。我很少使用按钮状态:相反,如果它们不活动,我会让它们消失。否则,没有经验的用户可能会在试图弄清楚如何激活它们时轻点或感到沮丧。
    【解决方案2】:

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

    - (void)buttonPressed:(id)sender
    

    把这个放在里面...

    - (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);
    }
    

    【讨论】:

      【解决方案3】:
      NSString * strButtonTitle = [btnButton titleForState:state];
      

      所在状态:

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

      根据您的要求

      NSString * strButtonTitle = [btnButton titleForState:UIControlStateSelected];
      

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

      【讨论】:

        猜你喜欢
        • 2012-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2016-04-30
        • 1970-01-01
        相关资源
        最近更新 更多