本文介绍了如何在按下UIButton时获取UIButton的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图在以下代码中发现UIButton标题是针对哪个UIButton被按下的。
I'm trying to discover what the UIButton title is for which UIButton was pressed in the following code.
在viewDidLoad上按钮标题输出到控制台使用:
on viewDidLoad the button title is outputted to the console using:
NSLog(@"The button title is %@ ", btn.titleLabel.text);
我想在按下按钮时获得此标题。
I would like to get this title when a button is pressed instead.
感谢您的帮助
:)
// 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的评论。
Edit:Or as commented Iulian: sender.currentTitle
, but it may be nil
, see the comments of Michael.
这篇关于如何在按下UIButton时获取UIButton的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!