2

I have a button:

- (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;
}

How can I change the title of the button with a tag from another class? I need to change the title of the button.

nwinkler
  • 52,665
  • 21
  • 154
  • 168
Alexander
  • 627
  • 2
  • 11
  • 23

3 Answers3

1

I didnt get what you meant by another class. If its from your view where button is added, then you can use the below code to get UIButton

UIButton *btn = (UIButton *)[self.contentView viewWithTag:ADRESS_SUBWAY_BTN_TAG];
[btn setTitle:@"New title" forState:UIControlStateNormal];
HRM
  • 2,097
  • 6
  • 23
  • 37
1

use delegates and @protocol to access parent view controller in ios refer tis simple example

link1

Community
  • 1
  • 1
NANNAV
  • 4,875
  • 4
  • 32
  • 50
1

You would need a reference to the other class in your view controller, declare the button in its header file and use the method -viewWithTage:

Example:

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];
Yuliani Noriega
  • 1,085
  • 12
  • 23