Try this code I have made the demo app for you.
create file CustomView.h
#import <UIKit/UIKit.h>
@interface CustomView : UIView
@property (nonatomic, strong) IBOutlet UILabel *titleLbl;
@end
CustomView.m. If you are using XIB
#import "CustomView.h"
@implementation CustomView
@synthesize titleLbl = _titleLbl;
- (id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder])
{
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
UIView *theEditView = [nibObjects objectAtIndex:0];
theEditView.frame = self.bounds;
[self addSubview: theEditView];
theEditView = nil;
}
return self;
}
Set fileOwner
of CustomView.XIB
is CustomView. and connect outlets.
Where ever you want to use CustomView
take a UIView
object in your XIB
, and rename UIView
class with CustomView
. Create an IBOutlet
in your .h
file and connect it with CustomView
object in XIB
.
Now do this:
self.customViewObj.backgroundColor = [UIColor redColor];
self.customViewObj.titleLbl.text = @"Prateek";
In your case your customview
object is not created. if you print your object it will show you nil
.