How do I connect a IBOutlet to a UIView?

2020-02-10 09:11发布

问题:

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.

回答1:

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!



回答2:

I am late to this. But let me post what I found out recently anyways.

I have this situation :

1) BaseViewController that contains MenuView(Custom UIView)

2) MenuView contains menuTable which holds all the menu items

3) On StoryBoard I have BaseViewController scene with MenuView and MenuTableView

I could have IBOutlet reference to menuTable that we normally do by dragging the table on storyboard to the BaseViewController.h. But I want custom MenuView to be responsible for populating the MenuTable as a separation concern. In order to do that I need IBOutlet reference connection from MenuView.h to the menu table on storyboard.

So this is what I did :

1) First create IBOutlet property of the table in MenuView.h @property (weak,nonatomic) IBOutlet UITableView *menuTable;

2) Once the property is created, there is a little circle just left side of the property declaration signifying that this is IBOutlet variable.

3) Now with StoryBoard open and MenuView.h open in Assistant Editor in XCode, I can click on the circle and drag it to table on the storyboard. (Note: If I try drag the table from StoryBoard to MenuView.h , it doesn't let me to. Note sure why)

That's it. Now I've made the IBOutlect connection from custom UiView to StoryBoard.

Note : I am using XCode 7 and targeting iOS 7 and above devices. (If that makes any difference)



回答3:

If you are new to Xcode or Storyboards than you should take a look at this basic Tutorial:

Beginning Storyboards in iOS 5 Part 1

Beginning Storyboards in iOS 5 Part 2



回答4:

Are you declaring the view as a property?

The syntax should be something like this:

@property (nonatomic, weak) IBOutlet UIView *main;