It will not reuse a single xib for multiple controllers. If you want to reuse this view, create a class that inherits from UIView and adds the code there.
#import "SomeProtocol.h" @interface MyCustomView : UIView { IBOutlet UIView *headerView; IBOutlet UIView *footerView; IBOutlet UIButton *updateBtn; } @property (nonatomic, assign) id<SomeProtocol> delegate; @end ............ @implementation BCFirmwareView @synthesize delegate = _delegate; + (id)viewFromNibWithName: (NSString*)name { UIView *view = nil; NSArray *views = [[NSBundle mainBundle] loadNibNamed: name owner: self options: nil]; if (views) { for (UIView *aView in views) { if ([aView isKindOfClass: NSClassFromString(name)]) view = aView; } } return view; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder: aDecoder]; if (self) { } return self; } - (id)init { self = [[MyCustomView viewFromNibWithName: @"MyCustomView"] retain]; if (self) { } return self; } - (void)dealloc { self.delegate = nil; [headerView release]; [footerView release]; [updateBtn release]; [super dealloc]; } - (void)awakeFromNib { [super awakeFromNib]; // Do any additional setup after loading the view from its nib. headerView.backgroundColor = [UIColor redColor]; footerView.backgroundColor = [UIColor greenColor]; } - (void)willMoveToSuperview:(UIView *)newSuperview { [super willMoveToSuperview: newSuperview]; if (!newSuperview) return; } - (void)didMoveToSuperview { [super didMoveToSuperview]; } - (IBAction)updateBtnPressed: (id)sender { // do some stuff } @end
The next step is to open xib in Interface Builder and set your class as a custom class for presentation, not for File Responder. Right-click on the view and make connections to the outlet and actions.
Now you can simply instantiate your MyCustomView in any view controller and use it. It will work from the Builder interface if you remember to change your custom view class in your class.
source share