Finding File Owner in Xcode 4.2
Asked Answered
M

4

16

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.

Question: What should i connect my Label to?

Storyboard: enter image description here

Meanwhile class is set up as

enter image description here

Musser answered 15/10, 2011 at 1:57 Comment(1)
I think you should accept the answer supplied by PREM instead.Bergstein
C
9

Right click the Label and connect to the View controller scene

Citrin answered 2/11, 2011 at 9:7 Comment(3)
Your answer is short, sharp and to the point. It should have been the accepted answer. However, i don't understand what "scene' means in this instance.Bergstein
A "scene" is newer terminology used to describe a controller in a storyboard.Balakirev
For those who are newer to storyboards, check out raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1 and this klanguedoc.hubpages.com/hub/… on how to connect IBOutlets and actions directly to your codeBalakirev
B
10

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.

Butternut answered 16/1, 2012 at 11:35 Comment(0)
C
9

Right click the Label and connect to the View controller scene

Citrin answered 2/11, 2011 at 9:7 Comment(3)
Your answer is short, sharp and to the point. It should have been the accepted answer. However, i don't understand what "scene' means in this instance.Bergstein
A "scene" is newer terminology used to describe a controller in a storyboard.Balakirev
For those who are newer to storyboards, check out raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1 and this klanguedoc.hubpages.com/hub/… on how to connect IBOutlets and actions directly to your codeBalakirev
C
4

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.

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.)

Calotte answered 12/11, 2011 at 0:35 Comment(1)
I don't quite get this answer. How do I access the label exactly? A piece of code would be most helpful. ThxAdeleadelheid
A
-1

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.

Automagically you will see something like this generated: @property (weak,nonatomic) IBOutlet UILabel *myLabel;

Inside the View Controler.m, you will see something like this generated: @synthesize *myLabel;

Inside your IBAction events, you can set the label: myLabel.text =

Ataghan answered 17/7, 2012 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.