xcode 访问在当前 ViewController 的故事板中创建的标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9050453/
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
Accessing a label created in the storyboard on current ViewController
提问by user1170665
I want to do something pretty simple but have yet am having a hard time doing so. I want to access a label created on the currently displayed ViewController (with storyboarding) from that view controller's class. Thanks for any help.
我想做一些非常简单的事情,但我很难做到。我想从该视图控制器的类访问在当前显示的 ViewController(带有故事板)上创建的标签。谢谢你的帮助。
采纳答案by LJ Wilson
MobileOverlord is right, but you should really start using the Assistant Editor for wiring up things like this. It will do ALL the work for you:
MobileOverlord 是对的,但你真的应该开始使用助理编辑器来连接这样的东西。它将为您完成所有工作:
- Declaring your property as an IBOutlet
- Synthesizing it for you in the implementation file
- Setting it to nil in the viewDidUnload method (which you will forget 90% of the time if you do it yourself)
- 宣布您的财产为 IBOutlet
- 在实现文件中为你综合
- 在 viewDidUnload 方法中将它设置为 nil(如果你自己做,你会忘记 90% 的时间)
If you have never used the Assistant Editor, you should start it is super easy and helps you concentrate on more important stuff than manually wiring up your outlets AND actions.
如果您从未使用过助理编辑器,那么您应该非常容易上手,它可以帮助您专注于比手动连接插座和操作更重要的事情。
回答by MobileOverlord
You need to make an IBOutlet Property in your viewcontroller.h and connect it to the label.
您需要在 viewcontroller.h 中创建一个 IBOutlet 属性并将其连接到标签。
After declaring the label property you can click and drag the o next to the property declaration (In Assistive Editor Mode) to the label to make the connection. Dont forget to synthesize it in the m.
声明标签属性后,您可以单击并拖动属性声明(在辅助编辑器模式下)旁边的 o 到标签以建立连接。不要忘记在 m 中合成它。
#import "ViewController.h"
@implementation ViewController
@synthesize myLabel;