2013-09-27 28 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