Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios get label from tag number

Tags:

I want to get a label's attributes from self so I tried:

UILabel *label = (UILabel *)[self viewWithTag:1]; 

But all I got was an empty UILabel instead of the label I was looking for. What is correct way to do this.

like image 968
J Max Avatar asked Oct 05 '11 16:10

J Max


2 Answers

So I figured out what I was doing wrong. Obviously

 UILabel *label = (UILabel *)[self viewWithTag:1]; 

creates a pointer to the instance of my label with tag 1. This does actually work. What doesn't work and why I was having problems came from trying to release label. Releasing label is the same as release [self viewWithTag:1] which is not what I was trying to do.

like image 135
J Max Avatar answered Oct 03 '22 07:10

J Max


In Objective C for getting UILabel in UIViewController by tag.

here i take assumption as your UILabel is added on self.view, otherwise you can pass your view where you added UILabel as [yourview viewWithTag:1];

UILabel *label = (UILabel *)[self.view viewWithTag:1]; 

In UITableView

UILabel *label = (UILabel *)[cell viewWithTag:1]; 

cell is a object of UITableViewCell as

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

In Swift 3

let label:UILabel = self.view.viewWithTag(1) as! UILabel 

In UITableView

let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell!  let label:UILabel = cell.viewWithTag(1) as! UILabel 

NOTE: before using tag (viewWithTag) there should be object (UILabel) with that tag number, otherwise app is crash.

you can set tag for any object via storyboard or via XIB and programatically.

like image 20
Nikhlesh Bagdiya Avatar answered Oct 03 '22 06:10

Nikhlesh Bagdiya