Finding File Owner in Xcode 4.2

17,598

Solution 1

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.

Solution 2

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

Share:
17,598
James Raitsev
Author by

James Raitsev

I ask a lot of questions. Some of them are good.

Updated on June 16, 2022

Comments

  • James Raitsev
    James Raitsev about 2 years

    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

  • Redfox
    Redfox over 12 years
    I don't quite get this answer. How do I access the label exactly? A piece of code would be most helpful. Thx