ios 从标签号中获取标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7664802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:44:45  来源:igfitidea点击:

ios get label from tag number

iosios4

提问by J Max

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

我想从 self 获取标签的属性,所以我尝试了:

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.

但我得到的只是一个空的 UILabel 而不是我正在寻找的标签。什么是正确的方法来做到这一点。

回答by J Max

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 labelis the same as release [self viewWithTag:1] which is not what I was trying to do.

创建一个指向我的标签实例的指针,标签为 1。这确实有效。什么不起作用以及为什么我遇到问题来自试图发布label。释放标签与释放 [self viewWithTag:1] 相同,这不是我想要做的。

回答by Nikhlesh Bagdiya

In Objective Cfor getting UILabel in UIViewController by tag.

Objective C 中,通过标签获取 UIViewController 中的 UILabel。

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

在这里,我假设您UILabel已添加self.view,否则您可以在添加UILabel为的地方传递您的视图[yourview viewWithTag:1];

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

In UITableView

UITableView

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

cell is a object of UITableViewCellas

单元格是UITableViewCellas的对象

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

In Swift 3

斯威夫特 3

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

In UITableView

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.

注意:在使用标签 (viewWithTag) 之前,应该有带有该标签编号的对象 (UILabel),否则应用程序会崩溃。

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

您可以通过故事板或通过 XIB 以编程方式为任何对象设置标签。

回答by Kevin ABRIOUX

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

对于 Swift 3,当我在视图控制器中时,我必须调用此代码以按标签检索视图:

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