在 Xcode 4.2 中查找文件所有者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7775256/
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
Finding File Owner in Xcode 4.2
提问by James Raitsev
Please glance at the image below and help me find a File Owner for the class.
请浏览下图并帮助我找到该课程的文件所有者。
Generally i would connect my UILabel to it, but, alas, i can't find it.
通常我会将我的 UILabel 连接到它,但是,唉,我找不到它。
Question:What should i connect my Label to?
问题:我应该将我的标签连接到什么?
Storyboard:
故事板:
Meanwhile class is set up as
同时班级设置为
采纳答案by James Raitsev
Right click the Label and connect to the View controller scene
右键单击标签并连接到视图控制器场景
回答by Bee
As storyboards don't have an owner, you can use the View Controller instead.
由于故事板没有所有者,您可以使用视图控制器代替。
Ctrl click (or right click) the label, drag the blue line to connect up with the orange View Controller.
Ctrl 单击(或右键单击)标签,拖动蓝线与橙色 View Controller 连接。
回答by matt
You have put your finger on a key difference between storyboards and nibs: when a nib is loaded, an owner instance is specified, but a storyboard is not loaded with an owner, so there is no file's owner in a storyboard. Your ViewController instance is created by the storyboard and is proxied in the scene (listed as View Controller), so you can draw a connection between that and an interface item. But if you want to form a connection with an already-existing instance not represented in the storyboard, you'll have to identify that instance in some other way (perhaps by a tag) and find it and runtime and form the connection in code after the storyboard loads.
您已经指出了故事板和笔尖之间的主要区别:加载笔尖时,指定了所有者实例,但故事板未加载所有者,因此故事板中没有文件的所有者。您的 ViewController 实例由情节提要创建并在场景中代理(列为视图控制器),因此您可以在该实例和界面项之间建立连接。但是,如果您想与故事板中未表示的已存在实例建立连接,则必须以其他方式(可能通过标签)识别该实例并找到它和运行时并在之后的代码中形成连接故事板加载。
For example, in this code, I manually load a storyboard (to use its initial scene in a popover) and then form connections from some bar button items within it:
例如,在此代码中,我手动加载故事板(以在弹出窗口中使用其初始场景),然后从其中的一些条形按钮项形成连接:
UINavigationController* nav =
(UINavigationController*)[[UIStoryboard storyboardWithName:@"Storyboard"
bundle:nil]
instantiateInitialViewController];
// there is no file's owner...
// so we can't just draw the connection from button items to ourself,
// because we are not proxied in the storyboard
// so, locate the button items in some other way and do it in code
UIViewController* root = [nav.viewControllers objectAtIndex: 0];
[root.navigationItem.leftBarButtonItem setTarget:self];
[root.navigationItem.leftBarButtonItem setAction:@selector(save:)];
[root.navigationItem.rightBarButtonItem setTarget:self];
[root.navigationItem.rightBarButtonItem setAction:@selector(cancel:)];
In some cases, there's a trick you can use to inject an arbitrary existing instance into a scene so that a connection to it will work: make that instance the first responder. There is a first responder proxy in every scene, so this can give you something to connect to by drawing within the storyboard. So, this code could work instead of the above:
在某些情况下,您可以使用一个技巧将任意现有实例注入场景中,以便与其建立连接:使该实例成为第一响应者。每个场景中都有一个第一响应者代理,因此这可以通过在情节提要中绘制来为您提供连接。所以,这段代码可以代替上面的代码工作:
[self becomeFirstResponder];
UINavigationController* nav =
(UINavigationController*)[[UIStoryboard storyboardWithName:@"Storyboard"
bundle:nil]
instantiateInitialViewController];
(And the button action connections have been drawn in the scene from each button to the first responder proxy object.)
(并且已经在场景中绘制了从每个按钮到第一响应者代理对象的按钮动作连接。)
回答by Drew
Menu: Navigate - Reveal in Project Navigator In the Project Navigator, Click on the "Main Storyboard" Menu: View - Show Assistant Editor You should have the Storyboard on the left with your label, and the view controler.h text on the right. Click on your label, hold down the control button, and drag a blue line to the View Controler.h source code on the right. Type in a reference name (for example myLabel), and click connect.
菜单:导航 - 在项目导航器中显示 在项目导航器中,单击“主情节提要”菜单:查看 - 显示助理编辑器左侧应该有带有标签的情节提要,右侧是视图 controler.h 文本。单击您的标签,按住控制按钮,然后将一条蓝线拖到右侧的 View Controler.h 源代码中。输入引用名称(例如 myLabel),然后单击连接。
Automagically you will see something like this generated: @property (weak,nonatomic) IBOutlet UILabel *myLabel;
你会自动看到这样的东西生成:@property (weak,nonatomic) IBOutlet UILabel *myLabel;
Inside the View Controler.m, you will see something like this generated: @synthesize *myLabel;
在 View Controler.m 中,您将看到生成的类似内容:@synthesize *myLabel;
Inside your IBAction events, you can set the label: myLabel.text =
在您的 IBAction 事件中,您可以设置标签:myLabel.text =