将字符串分配给 UITextView
我正在尝试将文本加载到文本视图中。这看起来很简单:
box.text = @"hello";
NSLog(@"Box.text contains: %@", box.text);
问题是这个 NSLog 一直打印 null。 (我声明了 IBOutlet UITextView *box。)我想由于 box.text 为空,这就是为什么 UITextView 中实际上没有显示任何内容。
有人知道出了什么问题吗?
多谢!
I am trying to load text into a textview. This seems simple enough:
box.text = @"hello";
NSLog(@"Box.text contains: %@", box.text);
The problem is that this NSLog just keeps printing null.
(I have IBOutlet UITextView *box declared.) I imagine that since box.text is null, that's why nothing is actually showing up in the UITextView either.
Anyone know what's going wrong?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从控制器的
init
方法调用box.text
,则 IBOutlet 尚未从 nib 文件加载 - 因此box
仍然指向 nil,并且文本赋值无处可去。您需要等到viewDidLoad
或更晚的时间才能成功访问 IBOutlet 的属性。If you're calling
box.text
from your controller'sinit
method, your IBOutlet hasn't been loaded from the nib file yet – sobox
is still pointing to nil, and the text assignment is going nowhere. You'll need to wait untilviewDidLoad
or later to successfully access the properties of your IBOutlet.