2013-02-15 30 views
2

我有一個按鈕:如何從iOS中的其他課程訪問標籤上的按鈕?

- (UIButton *)subwayABBtn 
{ 
    if (!_subwayABBtn) 
    { 
     _subwayABBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 

     _subwayABBtn.tag = ADRESS_SUBWAY_BTN_TAG; 

     [_subwayABBtn setTitle:@"Метро" forState:UIControlStateNormal]; 
     [_subwayABBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 

     [self.contentView addSubview:_subwayABBtn]; 
    } 

    return _subwayABBtn; 
} 

如何更改按鈕的標題從另一個類標籤?我需要改變按鈕的標題。

+1

什麼是「另一個類」,你通常使用'viewWithTag:' – 2013-02-15 13:07:16

回答

1

你需要參考視圖控制器中的其他類,在其頭文件中聲明該按鈕並使用方法-viewWithTage:

例如:

OtherClass *otherClass; // This should be set from previous class and a property in your current class 
UIButton *buttonFromOtherClass = [otherClass viewWithTag:BUTTON_TAG]; 
[buttonFromOtherClass setTitle:@"Метро" forState:UIControlStateNormal]; 
1

我沒有得到你的意思是另一個班級。如果從您的視圖的其中添加按鈕,然後就可以用下面的代碼來獲得的UIButton

UIButton *btn = (UIButton *)[self.contentView viewWithTag:ADRESS_SUBWAY_BTN_TAG]; 
[btn setTitle:@"New title" forState:UIControlStateNormal]; 
相關問題