xcode 如何更新 uilabel 文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3802051/
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
How to update a uilabel text
提问by iphoneStruggler
Hi in my project i need to update a label according to the events performed.
Suppose I'm interacting with the server then the label should display following text
1.Connecting to the server
2.Received response from the server etc
嗨,在我的项目中,我需要根据执行的事件更新标签。
假设我正在与服务器交互,那么标签应显示以下文本
1.Connecting to the server
2.Received response from the server etc
Can you help me?
你能帮助我吗?
回答by SK9
Your question could be more complete.
你的问题可能更完整。
If you're doing things programmatically you need to call the setText:
method on your instance of UILabel with the new message each time. Something like:
如果您以编程setText:
方式进行操作,则每次都需要使用新消息调用UILabel 实例上的方法。就像是:
//In practice use a smaller frame.
UILabel *label = [[UILabel alloc] initWithFrame:[window bounds]];
[label setText:@"Waiting for the server to do something interesting..."];
[window addSubview: label];
//later on....
[label setText:@"The server just sneezed! What shall I do?"];
回答by kidsid49
Update the label text whenever you want, and then call the setNeedsDisplay function on it:
随时更新标签文本,然后在其上调用 setNeedsDisplay 函数:
myLabel.text=@"Initial Text";
[myLabel setNeedsDisplay];
回答by Ank
You have to create the outlet of UILabel. and then set the "labelname.text" to what u want according to the event.
您必须创建 UILabel 的出口。然后根据事件将“labelname.text”设置为你想要的。
回答by Suresh Varma
you can set the text property of the label for setting text.
您可以设置标签的文本属性来设置文本。
For example: In While connecting to the server event:
例如:在连接到服务器事件时:
myLabel.text=@"Connecting to the server";
In the event When you recieve response
如果您收到回复
myLabel.text=@"Received response from the server";
and so on....
等等....
ADD LABEL THROUGH CODEThis is how to add the label through code because i cant show the binding here in .h file
新增标签通过代码这是如何通过代码添加标签,因为我不能显示在这里结合 在.h文件中
UILabel* myLabel;
in .m file viewDidLoad (Note: Dont alloc the label again in code except here)
在 .m 文件 viewDidLoad (注意:不要在代码中再次分配标签,除了这里)
myLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,10,200,40)];//SET THE FRAME ACCORDING TO YOUR USE
[self.view addSubview:myLabel];
myLabel.text=@"Initial Text";
Release the label
释放标签
- (void)dealloc {
[myLabel Release];
}