Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a custom subview (created in a xib) to a view controller's view - What am I doing wrong

I've created a view in a xib (with an activity indicator, a progress view and a label). Then I've created .h/.m files:

#import <UIKit/UIKit.h>  @interface MyCustomView : UIView {     IBOutlet UIActivityIndicatorView *actIndicator;     IBOutlet UIProgressView *progressBar;     IBOutlet UILabel *statusMsg; }  @end  #import "MyCustomView.h"  @implementation MyCustomView      - (id)initWithFrame:(CGRect)frame {     if ((self = [super initWithFrame:frame])) {         // Initialization code     }     return self; }  - (void)dealloc {     [super dealloc]; }  @end 

In IB, I set the file's owner and view identity to MyCustomView and connect the IBOutlet to the File's owner

In MyViewController.m, I've:

- (void)viewDidLoad {      [super viewDidLoad];         UIView *subView = [[MyCustomView alloc] initWithFrame:myTableView.frame];     [subView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];      [myTableView addSubview:subView];     [subView release]; } 

When I run the app, the view is added, but I can't see the label, the progress bar and the activity indicator.

What am I doing wrong?

like image 955
Sefran2 Avatar asked Mar 18 '11 16:03

Sefran2


People also ask

How do I load a custom view in storyboard?

Using a custom view in storyboardsOpen up your story board and drag a View (colored orange below for visibility) from the Object Library into your view controller. Set the view's custom class to your custom view's class. Create an outlet for the custom view in your view controller.

How do I add Xib in Objective C?

Input the cocoa touch class name, select it's superclass, and check the Also create XIB file checkbox, select the coding language that you use ( Swift or Objective-C), then click Next button. Select the xib file saved location in the next popup dialog, then click Create button to create them.

How do I load a view in Swift?

In the XIB, I changed the "File's Owner" class to SomeView (in the identity inspector). I created a UIView outlet in SomeView. swift, linking it to the top level view in the XIB file (named it "view" for convenience). I then added other outlets to other controls in the XIB file as needed.


2 Answers

You need to load it using the -loadNibNamed method. -initWithNibName is only for UIViewControllers.

Add the following code to your MyCustomView init method:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]; UIView *mainView = [subviewArray objectAtIndex:0]; [self addSubview:mainView]; 

Remember, if you are initializing an object from a nib, it calls - (id)initWithCoder:(NSCoder *)aDecoder to initialize, so you'll have to override that if you are creating the MyCustomView object within the nib. If you're just doing it with initWithFrame:, then just override that and add the code above. Also, in your nib, make sure you have one top-level UIView, and place all other elements within that (that makes sure that your subviewArray only has one entry).

This will load the views from the nib and add them to the object, and should do the trick.

like image 131
Ned Avatar answered Sep 20 '22 20:09

Ned


I think you need to use this method:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil; 

This is because you need to pass it the .xib filename in the "nibNameOrNil".

like image 42
hocker Avatar answered Sep 20 '22 20:09

hocker