为什么我不能更改我的UIButton上的标签?
我有一个NSString像“S21”,并试图将数字转换为按钮的标记。为什么我不能更改我的UIButton上的标签?
我做:
NSString *temp = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@"s" withString:@""];
[_b0 setTag: [temp intValue]];
_B0是我的UIButton。
我运行的NSLog检查标签我跑setTag之后,但在按钮的标签并没有改变,仍然为0。
我在做什么错?
您只需查看UIButton
的IBOutlet
连接,如果你在创建过程通过nib文件。
NSString *str = @"S21";
NSString *newStr = [str substringFromIndex:0];
这应该工作
NSString *temp = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@"s" withString:@""];
_b0.tag = [temp intValue];
你的代码是正确的,没有问题,因为我在我的最终实现。见下面我想你正在做的事情错在其他代码..
NSString *temp = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@"s" withString:@""];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20, 20, 100, 100);
[self.view addSubview:btn];
[btn setTag: [temp intValue]];
NSLog(@"Tag %d",btn.tag);
输出:
标签21
其他的东西肯定是错的。我的错。对不起浪费大家时间。今天早些时候我复位了我的笔尖,并忘记将这个按钮与_b0重新关联。谢谢你们的帮助。 – 2013-05-13 08:06:06
非常欢迎我亲爱的,我希望你有你的答案。 – 2013-05-13 08:07:51
@EdEdEddyChen检查我的答案哥们.... – Rajneesh071 2013-05-13 08:10:00
*temp = [[array objectAtIndex:0] stringByReplacingOccurrencesOfString:@"s" withString:@""];
_b0.tag = [temp intValue];
记录'temp'的值并检查它是否正确。 – trojanfoe 2013-05-13 07:53:16
NSLog记录'temp'并查看它有什么,还有'_b0'以确保它不是'nil'。在设置标签后也可以设置NSLog(@“%d”,_ b0.tag)“。让我们知道。 – samfisher 2013-05-13 07:54:04
temp设置标签前后显示21,_b0显示0。 – 2013-05-13 07:58:20