Adding header view - why does this work ? I Need clarity


#1

Hi folks - just trying to understand why this code from the book should work.

i think i’ve worked it out with the help of another forum post here but need to know if i’m right.

(its location 5452 in the digital edition- for those with the print editing its just after figure 10.6 -the color picker)

-(UIView *) headerView
{
    // if we havent loaded the headerview yet...
    if(!headerView){
        //load the HeaderView.xib
        [[NSBundle mainBundle]loadNibNamed:@"HeaderView" owner:self options:nil];
        
    } return headerView;
    
}

2nd paragraph below the code in the book is the following paragraph …

Why does it keep a pointer to the view object in the variable headerView?

From this code I cannot see the connection between loading the view and it somehow having been assigned to the variable headerView and thus able to be returned as headerView - if that is indeed whats happening here.

is it that the connections shown in figure 10.5 is being used to assign a pointer to the headerView variable when the HeaderView.xib file is loaded ? then the headerView variable can be returned If it is then I’ve got it.

The book stated that we were going “To load a Xib file manually” . i took that to mean that we wouldnt be connecting it up in interface builder yet it would appear to need us to do that.

if we were going to do this without dragging a connection in interface builder - what would that code look like?

thanks in advance for any helpful comments

Simon


#2

I’m a little blurry but i’ll give my 2 cents as to what i think is going on.

We made the UIView in the .xib file to connect to the ItemsViewController’s outlet called headerView - this is where the connection is set, and how the variable is set to point to that view.

But even though doing this, we still need to actually go and get the .xib file from the bundle, and when we do that, the connections are at that time set up to point to the IBActions and outlets.

Anyway, someone else might correct me lol xD


#3

Seerex is correct. You make the connection between the File’s Owner (a proxy object representing the view controller in this case) and the header view in the XIB.

When you load the XIB file with NSBundle notice that you pass the view controller as the owner:

When the nib (XIB becomes a NIB when compiled) file is loaded the connections you declared are then hooked up. That’s when the headerView ivar gets set.


#4

[quote=“silvermana”]Seerex is correct. You make the connection between the File’s Owner (a proxy object representing the view controller in this case) and the header view in the XIB.

When you load the XIB file with NSBundle notice that you pass the view controller as the owner:

When the nib (XIB becomes a NIB when compiled) file is loaded the connections you declared are then hooked up. That’s when the headerView ivar gets set.[/quote]

Why do we have to set the File Owner twice?
One time from the XIB file, and another time, from the code as above (when we set owner:self)? Can someone please clarify?


#5

The reason you have to pass self as the owner and also connect headerView ivar in the UIBuilder to the View is this:

  • a XIB file specifies a class for the file’s owner (ItemsViewController) and an instance variable in that class (headerView)
  • at runtime, passing a reference to self as the owner then says that this is an instance of the owner class and now the links can be completed