xcode UILabel 的值设置问题(iPhone SDK)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2112588/
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
Problem setting Value for UILabel (iPhone SDK)
提问by Michael
I'm reasonably new at the iPhone SDK, so please forgive me if this answer's ridiculously obvious.
我是 iPhone SDK 的新手,所以如果这个答案太明显了,请原谅我。
I have a ViewController connected to a .xib file (they are called FirstViewController)
我有一个连接到 .xib 文件的 ViewController(它们被称为 FirstViewController)
I also have a custom class called MyCustomClass. In this class, I have a method like this:
我还有一个名为 MyCustomClass 的自定义类。在这个类中,我有一个这样的方法:
- (void)setTheText {
[myLabel setText:@"foo"];
}
I want to call this method to set the text in a label in FirstViewController.xib
我想调用这个方法在 FirstViewController.xib 的标签中设置文本
So far, I have done this:
到目前为止,我已经这样做了:
Dragged in an "Object" into Interface Builder, and set the class to: MyCustomClass Connected up the IBOutlet 'myLabel' from MyCustomClass to a UILabel in the view.
将“对象”拖入 Interface Builder,并将类设置为: MyCustomClass 将 IBOutlet 'myLabel' 从 MyCustomClass 连接到视图中的 UILabel。
However, when I run the program, and press the button to set the label (which is in FirstViewController.m), something like:
但是,当我运行程序时,按下按钮设置标签(在 FirstViewController.m 中),例如:
- (IBAction)doSomething:(id)sender {
MyCustomClass *customClass = [MyCustomClass alloc] init];
[customClass setTheText];
}
This doesn't set though. NSLog(@"%@",[myLabel text]);
returns (null)
但这并没有设置。NSLog(@"%@",[myLabel text]);
返回(空)
Xcode shows no errors or warnings, what could be wrong?
Xcode 没有显示错误或警告,可能有什么问题?
Thanks,
谢谢,
Michael
迈克尔
Additional Info:
附加信息:
Interface to MyCustomClass.h:
MyCustomClass.h 的接口:
#import <UIKit/UIKit.h>
@interface MyCustomClass : NSObject {
UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
- (void)setTheText;
@end
回答by e.James
You don't want to create a new instance of your custom class in your action method.
您不想在操作方法中创建自定义类的新实例。
There are a couple of ways to do what you want.
有几种方法可以做你想做的事。
Option 1is to give your view controller a reference to your custom object. To do this, create an outlet of type MyCustomClass *
in your view controller, connect that outlet to the new object that you created in your XIB file, and then get rid of the allocation in your action method:
选项 1是为您的视图控制器提供对您的自定义对象的引用。为此,请MyCustomClass *
在您的视图控制器中创建一个类型为 outlet 的 outlet ,将该 outlet 连接到您在 XIB 文件中创建的新对象,然后在您的操作方法中摆脱分配:
- (IBAction)doSomething:(id)sender {
[customClass setTheText];
}
Option 2is to let your CustomClass
handle both the label andthe action method. To do this, you can simplify things even further. Put an outlet for the UILabel
into your CustomClass
, and then simply convert your setTheText
method into an action:
选项 2是让您同时CustomClass
处理标签和操作方法。为此,您可以进一步简化事情。将 的出口UILabel
放入您的CustomClass
,然后只需将您的setTheText
方法转换为操作:
- (IBAction)setTheText:(id)sender {
[myLabel setText:@"foo"];
}
Now, connect that action up to your button, and everything should work like a charm.
现在,将该操作连接到您的按钮,一切都应该像魅力一样工作。
Note:You should probably use a method name that does not start with "set", since those are commonly used for property setters as part of Cocoa's KVC/KVO system. Instead, I would call it something like changeLabel
, or equivalent.
注意:您可能应该使用不以“set”开头的方法名称,因为它们通常用于属性设置器,作为 Cocoa 的 KVC/KVO 系统的一部分。相反,我会称它为类似changeLabel
,或等价物。
回答by Rob Napier
Your instance of customClass here is completely unrelated to the NIB. You've instantiated a new object using +alloc. If you want to modify the specific MyCustomClass in your NIB, then FirstViewController needs an IBOutlet that points to it.
您在此处的 customClass 实例与 NIB 完全无关。您已经使用 +alloc 实例化了一个新对象。如果您想修改 NIB 中的特定 MyCustomClass,则 FirstViewController 需要一个指向它的 IBOutlet。
回答by Convolution
UILabel also have a property called text to set the value, you might find that easier.
UILabel 也有一个名为 text 的属性来设置值,您可能会发现这更容易。
myObject.myLabel.text = @"Your text";
回答by Eman yalpsid
well you called your label two things, first a non-ibaction then an ibaction in the property.
好吧,您将标签称为两件事,首先是非 ibaction,然后是属性中的 ibaction。