xcode 更改标签文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9911518/
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
Change Label Text
提问by Qasem
How can I change a label's text? This is what I have tried.
如何更改标签的文本?这是我尝试过的。
labelCheckBox.text = @"This is the new label";
I connected the label in Interface builder for labelCheckBox and created an outlet for it. In the Didload the label's text changed, but in another method inside the class it does not have the new value.
我在接口构建器中为 labelCheckBox 连接了标签并为其创建了一个插座。在 Didload 中,标签的文本已更改,但在类中的另一个方法中,它没有新值。
-(void)settext:(NSString*)newLabelText{
labelCheckBox.text = newLabelText;
// labelCheckBox = [[UILabel alloc] init];
// labelCheckBox.text = @"This is the new label";
}
I am calling the method in another class as shown here:
我正在另一个类中调用该方法,如下所示:
[New settext:@"All Transaction"];
The value of 'NewLabelText
' after printing it in NSLog is: " All Transaction
". But when I assign ' LabelCheckBox.Text = NewLabelText
', I print the value of 'LabelCheckBox.Text
',
and it gives empty.
' NewLabelText
' 在 NSLog 中打印后的值是:“ All Transaction
”。但是当我分配 ' LabelCheckBox.Text = NewLabelText
' 时,我打印了 ' LabelCheckBox.Text
'的值,它给出了空值。
回答by RomanHouse
First of all if you use IB you need to create outlet and connection for your UILabel. The easiest way it's dragging from your label (control + drag) to your .h file of controller class. Then synthesize it in .m file and now you can change label text as you do this in question.
首先,如果您使用 IB,您需要为 UILabel 创建插座和连接。最简单的方法是从您的标签(控制 + 拖动)拖动到控制器类的 .h 文件。然后将其合成到 .m 文件中,现在您可以在执行此操作时更改标签文本。
回答by Qasem
the 'View' should load before setting the label text.
“视图”应该在设置标签文本之前加载。
回答by J-Techsha
iOS development sometimes gives you the silence treatment like Javascript to help prevent your app from crashing. With that said, if you have created the UILabel
in Interface Builder and have set an IBOutlet
for it, make sure you change the text only after the UILabel
is created. (i.e. after -viewDidLoad
). You can set a break point to where you set the text change and a breakpoint at -viewDidLoad
in the UIViewController
that contains the UILabel
to confirm the order of calls that's taking place.
iOS 开发有时会像 Javascript 一样为您提供静音处理,以帮助防止您的应用程序崩溃。话虽如此,如果您UILabel
在 Interface Builder 中创建了并IBOutlet
为其设置了,请确保仅在UILabel
创建后更改文本。(即之后-viewDidLoad
)。您可以在设置文本更改的位置设置断点,-viewDidLoad
并在UIViewController
包含 的位置设置断点UILabel
以确认正在发生的调用顺序。
回答by Ketan Patel
Below code will change text in label with tag value
下面的代码将使用标签值更改标签中的文本
In scrollview
use this type to change the text in label
在scrollview
使用这种类型更改标签中的文本
for(UIView *views in ScrollViewMain.subviews)
{
UILabel *label = [views viewWithTag:[sender tag]+1000];
label.text=str1;
}
It will really work. thank you
它真的会起作用。谢谢你