objective-c - UILabel带有标签,无法设置文本

标签 objective-c uiview uilabel

我遇到标签问题。我有几个标签需要在标签的帮助下进行更改。我在我的代码中这样做,但它给出了一个异常(exception) -[UIView setText:]:无法识别的选择器发送到实例 这是我的代码:

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

label.text = @"empty";

感谢任何帮助,谢谢。

最佳答案

每个 View 的标签默认为0。

tag

An integer that you can use to identify view objects in your application.

@property(nonatomic) NSInteger tag

Discussion

The default value is 0. You can set the value of this tag and use that value to identify the view later.

您的viewWithTag将返回您的self.view。因此,您将得到一个 UIView 而不是 UILabel 并且您的应用程序将崩溃。请参阅viewWithTag的定义:

viewWithTag:

Returns the view whose tag matches the specified value.

- (UIView *)viewWithTag:(NSInteger)tag Parameters

tag

The tag value to search for. Return Value The view in the receiver’s hierarchy whose tag property matches the value in the tag parameter.

Discussion

This method searches the current view and all of its subviews for the specified view.

因此,您的解决方案是您需要为每个标签指定唯一的标签。然后使用 viewWithTag: 并给出您指定的标签。

关于objective-c - UILabel带有标签,无法设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13682046/

相关文章:

ios - 屏幕边界扩展可见区域

ios - 如何使用 animateWithDuration 淡化标签的背景颜色

ios - 向 UILabel 添加手势

ios - 如何从 youtube 下载视频并保存( objective-c )?

iphone - 在遇到性能问题之前,iOS 上可以同时显示多少个 UIView?

ios - 声明字段/变量的位置之间的差异

ios - UITextView 和 UILabel swift 排成一行

objective-c - cocoa 和OpenGL : failed to draw cube

iphone - 控制台错误 : failed to find PDF header: `%PDF' not found

iphone - NSRangeException 错误