gpt4 book ai didi

iphone - 按下时如何获取 UIButton 的标题

转载 作者:技术小花猫 更新时间:2023-10-29 10:41:27 29 4
gpt4 key购买 nike

我试图找出在以下代码中按下 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);

}

最佳答案

在你的行动中:

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

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

关于iphone - 按下时如何获取 UIButton 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14199256/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com