Custom XIB File Owner

If I create a separate nib file and upload it to my view manager and assign it to ivar as follows:

self.loadingview=[[[NSBundle mainBundle] loadNibNamed:@"loading" owner:self options:nil] objectAtIndex:0];

Is it possible to create IBOutlets for any thing on it in a loop, for example, activity indicators? Would the viewcontroller be the file host, although IB would not know this since it became an object in the code?

Is loading a needle the way I made it an acceptable practice?

Perhaps a more acceptable practice is to create .h / .m and use this as a class for XIB, in which case File Owner is a new custom class; I still have to use the above method in my controller, although when creating ivar from a user class, right? Or is there a better way to do all this without using an xib controller?

UPDATE. Based on some answers, I did the following. I got rid of the NSBundle method above, and instead do the following:

@property (nonatomic, retain) IBOutlet UIView*loadingview;

In the nib file for this view, I click "File Owner" and change the Custom Class to my view manager. Then I control the drag and drop from File Owner to View and it binds perfectly.

I would suggest at this point that I now have ivar in my controller, self.loadingview , which contains information in nib. However, it is not. I removed the NSBundle method, assuming that the controller will now load the thread directly through IB output, but the view in the tip is not displayed, and I do not control it in the controller.

I also deleted this when I deleted the NSBundle method:

[self.view addSubview:self.loadingview];

I do not understand how the view is actually displayed; and if that happens, then where does the hierarchy appear to be? perhaps this is for other views. I also tried:

[self.view bringSubviewToFront:self.loadingview];

But there is simply no vision. Trying to find him, I did:

//[self.view bringSubviewToFront:self.loadingview];

NSLog(@"index: %i",[[self.view subviews]indexOfObject:self.loadingview]);

both with and without comment. I get the following:

index: 2147483647

+6
source share
3 answers

Just because you created a tip, and inside that tip, connected to a socket on the File Owner, it doesn't load automatically from your controller.

You can use the UINib class for UINib :

 UINib* nib = [UINib nibWithNibName:@"loading" bundle:[NSBundle mainBundle]]; [nib instantiateWithOwner:self options:nil]; 

Once you make instantiateWithOwner:self , the pin will be loaded, and self will be used as the owner of the file, and the view from nib will be associated with your loadingView property.

You still need to call [self.view addSubview:self.loadingView] to add it to the screen.

+6
source

The Builder interface recognizes the owner of the file sent through the owner:self argument. What you need to do to show IBOutlet in the Builder interface in order to set the correct file owner class in IB on the "Identification and type" tab.

+1
source

Is it possible to create IBOutlets for any thing on it in a loop, for example, activity indicators? Would the viewcontroller be the file host, although IB would not know this since it became an object in the code?

They will not be IBOutlets, but yes, you can connect the wiring manually. IBOutlet is an empty macro. It exists solely so that the Builder interface can find the properties that you would like to connect. But the IBOutlet keyword has absolutely no effect on runtime (this is not even visible to the compiler). Similarly, IBAction is a macro that is replaced by void . The Builder interface does not exist at run time.

When the nib bootloader downloads the nib file, it deserializes each object, and then for each connection, it sends a setter ( setFoo: message to the connection target that passes the new object. Upon completion of this, for each object in the nib file, it sends awakeFromNib to each object it awakeFromNib . Objects for the purpose of connecting nil sent to the "owner" (which in your example is self ). Note that the file owner is not created by the nib loader. The nib loader does not have control over the actual file owner class. You must make sure that the owner of the file answers the calls to set...: or you work.

Is loading a needle the way I made it an acceptable practice?

I would not rely on loadingView , which is the first top-level object. The order of the objects is not defined. Instead, you should use IB to connect the view to the loadingView property of the loadingView owner.

Perhaps a more acceptable practice is to create .h / .m and use this as a class for XIB, in which case File Owner is a new custom class; I still have to use the above method in my controller, although when creating ivar from a user class, right? Or is there a better way to do all this without using an xib controller?

The traditional way to do this is to have a UIViewController for every nib file in iOS. But it is not required. On Mac, NSViewController fairly new and does not work the same as UIViewController , so there is no such strong tradition. Manual loading, as I describe, is more common there, although most things are still handled through NSWindowController .

If you want to do such things, be sure to read the Nib Files in the Resource Programming Guide.

+1
source

Source: https://habr.com/ru/post/895874/


All Articles