2013-09-27 29 views
0

我想给一个字符串值UIButton标记唯一标识。 我试过这段代码,但它不起作用。将字符串分配给UIButton的标记值

NSString *A; 
    NSString *B; 
    NSString *C; 
    NSString *D; 

    firstOptionABtn.tag=[A integerValue]; 
    secondOptonBbtn.tag=[B integerValue]; 
    thirdOptionCbtn.tag=[C integerValue]; 
    fourthOptionDbtn.tag=[D integerValue]; 

    - (IBAction)BtnClicked:(UIButton *)sender { 
    NSLog(@"%ld",(long)sender.tag); 
    } 

我不喜欢这种方式,但它每次都会打印0。我哪里错了? 请帮忙。由于

+0

downvoter为什么downvoting我。的[如何字符串传递作为的UIButton的标签] – Jitendra

+0

可能重复(http://stackoverflow.com/questions/9632040/how-to-pass-a-string-as-a-tag-of-uibutton) –

回答

0

什么需要使用字符串键的设定标签值...为什么不设置按钮的标签静态简单的设置,如: -

firstOptionABtn.tag=1; 

看到此设置标签用绳子第一次为int转换

在你queston首先是你没有,如果你设置的字符串值一样

塞汀你串

NSString *A; 
NSString * B; 
NSString * C; 
NSString * D; 

的价值

[email protected]"2" 

那么你的代码工作,你有在按钮点击事件值

firstOptionABtn.tag=[A integerValue]; 

其输出

发送方标签2

进入你的字符串值考虑任何号码价值然后秘密字符串Intiger cosider唯一数字不字母字符像

例如

如果设置象A = @“2aaabbb”按钮,然后标签的输出为但是如果设置字符串值像A = @“bb2aaabbb”,那么输出字符串的值按钮标签始终为0考虑。

UPDATE

,如果你想设置的字符为int 可以转换NSString to ASCII像娄

[email protected]"A" 
int ASCIINumber = [A characterAtIndex:0]; 
button.tag=ASCIINumber; 

输出为: - 65大写如果小写输出为97

但正如我使用上面的方法,我建议你你只需简单地设置tag=intNumber而不是以上。

+0

亚我知道标记值总是存储在整数,但我只是为了解这就是为什么我问这个问题。 – Jitendra

+0

我想要这样的输出A,B,C,D ...... – Jitendra

+0

如果您尝试将字符串转换为整数,ABCD不会被视为返回值始终为0的整数值。所以更好使用int数值。检查我更新的答案,我解释完全 –

0

亲爱的在标签中设置字符串是不可能的。正如你所说,标签只需要int值。

0

创建一个由UIButtonNSString属性组成的自定义视图或将作为tag的ivar。

@interface MyButton: UIView 

@property(nonatomic, strong) UIButton *button; 
@property(nonatomic, strong) NSString *uniqueTag; 

@end 

希望帮助!

3

它返回零,你没有初始化字符串,它具有空值,当您转换成整数,它会返回零。而且你似乎从NIB创建你的按钮,所以你可以从那里设置它们的标签值,然后通过实现来获得价值。

-(IBAction)buttonClicked:(id)sender 
{ 
    UIButton *btn= (UIButton *)sender; 
    NSLog(@"%d",btn.tag); 
} 

但如果u希望输出A,B,C,d那么还有一个办法

-(void)viewDidLoad{ 

    [email protected]"A"; 
    [email protected]"B"; 
    [email protected]"C"; 
    [email protected]"D"; 

} 

-(IBAction)buttonClicked:(id)sender { 

    UIButton *btn= (UIButton *)sender; 
    NSLog(@"%@",btn.accessibilityLabel); 
} 

输出为A,B,C,d

+0

非常感谢。它为我工作:) –

0

我们可以做一个标记字符串类别UIButton

@interface UIButton (setTag) 

@property(nonatomic, retain) NSString *tagString; 

@end 

而在你的文件中的任何地方,你可以标记字符串设置为您的UIButton。 如

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 

[btn setTagString:@"Hello"]; 
0

你总是可以创建一个包含字符串属性与所述类

例如一起去自定义类:创建一个自定义类的UIButton

,然后在头文件放:

@property (nonatomic, strong)NSString *labelTag; 

- (id)initWithFrame:(CGRect)frame withLabelTag:(NSString *)str; 

然后,在实现创建一个超帧的方法,所以它看起来像这样:

- (id)initWithFrame:(CGRect)frame withLabelTag:(NSString *)str{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     //Init code 
     _labelTag = str; 
    } 
    return self; 
} 

后,每当您要设置或调用按钮的字符串标签,你只需要声明它并调用它像这样:

MyCustomButton *button = [[MyCustomButton alloc]initWithFrame:frame withLabelTag:@"this is the title"]; 

//and to call it : 
NSLog (@"the button's tag is :%@", button.labelTag) 

,或者你总是可以只使用按钮的title属性,如果你不希望它出现在您的按钮,只需将其隐藏的道具是

0

有两种情况。

案例1:如果您的代码串只包含数字 - (如:tagStringValue = "1433"

使用myButton.tag = [tagStringValue intValue];

找回使用

UIButton *myButton = ([[self.view viewWithTag:[tagStringValue intValue]] isKindOfClass:[UIButton class]])?(UIButton *)[self.view viewWithTag:[tagStringValue intValue]]:nil; 

案例2按钮:如果您的标签字符串包含数字和字符串 - (例如:tagStringValue = "ss1433kk"

子类的UIButton并添加一个属性,如:

@property (nonatomic, strong) NSString *stringTag;并使用此代替默认Tag