我遇到标签问题。我有几个标签需要在标签的帮助下进行更改。我在我的代码中这样做,但它给出了一个异常(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
Parameterstag
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/