How do I connect a IBOutlet to a UIView?

Jon Sullivan picture Jon Sullivan · Jan 29, 2013 · Viewed 20.2k times · Source

I'm trying to switch views after an animation as seen:

[UIView beginAnimations: @"Fade Out" context:nil];
[UIView setAnimationDelay:1.0];
[UIView setAnimationDuration:0.25];
splash.alpha = 0.0;
[UIView commitAnimations];
[self.view addSubview : view];

At the [self.view addSubview : view]; I have to add a name for the view for it to add, so I made an IBOutlet like so: (on the first view controller's .h file)

IBOutlet UIView *main;

But when I try to connect the *main UIView to the view on the storyboard, it wont let me... Thanks so much guys.

Answer

foundry picture foundry · Jan 29, 2013

Your point of confusion is between creating UI objects in code vs creating them graphically using Interface Builder / storyboard.

One clue is your use of the preprocessor hint 'IBOutlet'. IB is for Interface Builder == graphic creation (using a storyboard or xib file).

If creating in storyboard...

  • create an IBOutlet property as you have done, although the full correct syntax is
    @property (nonatomic, weak) IBOutlet UIView *main;

  • drag a "custom view" view from the object library to your storyboard scene.

  • CTRL-drag from the view to the viewController. You should get presented with a list of suitable items to connect to. Your IBOutlet should be in the list. select it.

The entire purpose of an IBOutlet is to give you a reference to an item in your storyboard scene that you can use in your code.

You won't need to do this:

  [self.view addSubview : view];

as it is already created and added in your storyboard. Make sure it is located as you expect in your view hierarchy.

If creating in code...

Declare a property in your @interface

  @property (nonatomic, strong) UIView *main;

(No need for IBOutlet as you aren't linking it up in your storyboard. Declared 'strong' instead of 'weak' in case you don't immediately assign it to a view hierarchy).

Before you add this line

  [self.view addSubview : view];

you need to consider: are you adding a new view that does not exist in your storyboard/xib? If so, you cannot add it until you have created it. (Adding a view that IS in your storyboard doesn't make sense, as it is already there, in your storyboard scene).

So - as you appear to be doing this in code - you need to create the view before adding it.

  UIView* myView = [[UIView alloc] init];

set it's frame property so that we know where it will appear when you add it to the view hierarchy

  myView.frame = CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height);

Assign your newly-created view to the property

  self.main = myView;

Add it to your view hierarchy

  [self.view addSubview : myView];

Now you can refer to it in code by using self.main. This is just as it would be if you were to have added it in IB/storyboard and CRTL-dragged a reference link to your IBOutlet.

If you are creating your view in code and immediately adding it to a view hierarchy, an alternative to declaring a property is to set the (numerical) tag property on the view, then you can refer to the view using it's superview's viewWithTag: method

The one thing you can't do is create the view in code, then manipulate it using the storyboard.

I hope this is all useful, I fear I may be confusing you further!