34

I want to get back a button, I've already assign the tag to there, how can I do so?

Cœur
  • 37,241
  • 25
  • 195
  • 267
DNB5brims
  • 29,344
  • 50
  • 131
  • 195

3 Answers3

90

Use the viewWithTag method. e.g. if your button is in your controller's view and your button's tag is 100 the following line will return the button:

UIButton *button = (UIButton *)[self.view viewWithTag:100];

EDIT:

Get view with particular tag in Swift -

let view = self.view.viewWithTag(100)

If you want to make sure you have the specific type of view, say a UIButton, you should check the type:

if let button = self.view.viewWithTag(100) as? UIButton {
    //Your code
}
Swapnil Luktuke
  • 10,385
  • 2
  • 35
  • 58
5
UIButton *button=(UIButton *)[self.view viewWithTag:tag]; 

//Now you can get your button based on tag value

Sugan S
  • 1,782
  • 7
  • 25
  • 47
vntstudy
  • 2,038
  • 20
  • 24
0
//Get View using tag
        UITextField *textFieldInView = (UITextField*)[self.view viewWithTag:sender.tag+1000];
        UILabel *labelInView = (UILabel*)[self.view viewWithTag:sender.tag+2000];

//Print its content based on the tags
        NSLog(@"The tag is %d ", textFieldInView.tag);
        NSLog(@"The tag is %d ", labelInView.tag);
        NSLog(@"The Content is %@ ", textFieldInView.text);
        NSLog(@"The Content is  %@ ", labelInView.text);
TEJAS PATELIA
  • 31
  • 1
  • 3