开发者

ios get label from tag number

I wa开发者_开发知识库nt 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.


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.


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.


For Swift 3, when i am in a view controller, i have to call this code to retrieve a view by tag :

self.mInstructionView = self.view.viewWithTag(1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜