Download XIB file without UIViewController

I would like to create a UIView and some subviews (UIWebView, UIToolbar, some UIBarButtonItems, progress indicator, etc.) using Interface Builder, but I think it is not necessary to do this traditionally using the UIViewController using presentViewController:animated and etc.

So, I created my own class with the .h file code as follows:

 @interface FileInteractionManager : NSObject { } @property (nonatomic, retain) IBOutlet UIView *fileView; @property (nonatomic, retain) IBOutlet UIWebView *fileWebView; @property (nonatomic, retain) IBOutlet UIBarButtonItem *printButton; @property (nonatomic, retain) IBOutlet UIBarButtonItem *optionsButton; @property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton; 

My .m file looks like this:

 @implementation FileInteractionManager @synthesize fileView, fileWebView, doneButton, optionsButton, printButton; -(id)init { NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FileInteractionView" owner:self options:nil]; NSLog(@"Load success!"); return self; } 

Finally, I create a standalone xib file named "FileInteractionView.xib", change the owner of the file to the user class that I created above, and enable IBOutlets.

When I call the init method in my class, I can see in the debugger that all my IBOutlet objects are created correctly.

My questions:

  • Is the loadNibNamed:owner:options: method the correct way to load my standalone .xib file? I do not like the fact that this method returns an array for which I do not use (the returned top-level object matches my fileView variable, but I already linked them using Interface Builder).

  • Is my general approach right when solving my problem? I followed the above steps because I need a simple UIView object that I could add to an existing UIViewController, instead of representing and rejecting the whole new UIViewController.

+4
source share
2 answers

I use a slightly different approach. I create a subclass of UIView (MyCustomView ie), then xib with the user interface of the view, and change the class (main) class just defined. In xib, you can associate the outlet with the user view itself (and not with the file owner). Finally, in the class definition, I create a function like this:

 + (id) newFromNib { NSArray *nibArray = [[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil] instantiateWithOwner:nil options:nil]; return nibArray[0]; } 

Just a few notes: 1) this is a class method, you can use the “I” only for things like “NSStringFromClass ([self class])”, but the real object is the returned variable 2) in this example, assume that xib has one and same class name (via NSStringFromClass ([self class]), so I can copy it without changing anything;)) and that your view is the first one defined in xib (standard). If you store more than the "main" view inside the same xib, select the correct item.

so i need mycustomview. I am doing something like:

 MyCustomView* mycv = [MyCustomView newFromNib]; 

then set frame / center and add to superiew ... I think this method is very useful if you have a "library" of complex user interface elements and you want to create them through xib, and then add them if necessary.

+6
source

il Malvaggio Dottor Prosciutto responds well. Here is a possible alternative.

Upload the thread to NS_DESIGNATED_INITIALIZER and become the owner of subview

If we accept xib only for storing the view, and not for presentation, then we can load the view in initWithFrame: and save the property construct in xib .

 @interface MyCustomView () @property (strong, nonatomic) IBOutlet UIView *subview; @end @implementation MyCustomView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]; [self addSubview:self.subview]; return self; } @end 
+1
source

All Articles