this is a follow-up to this question (custom UIView how-to with IB / Xcode 4.5.1 and iOS Simulator 6.0) but not necessary to read - this is my first time trying to create a custom UIView and there is clearly something that I'm not getting so thx in advance for any help that you can provide.
I have a custom UIView that is derived from Interface Builder. I want to set the set the size to 200w x 200h and the backgroundColor to Green. When I created I did the following:
- Created a new Custom UIView via File -> New -> Objective-C Class called Tview
- Created a new view via File -> New -> User Interface -> View and called it tnib
- in the Canvas, I deleted out the View and added a new View from the object inspector and set the class name to Tview. Also, in the Attributes inspector, I set the size to Freeform and the background color to Green. In the Size Inspector, I set the width to 200 and the height to 200.
In my Tview.m I set up with the following (I have made some updates based upon other SO questions but it is unclear whether those are still current or accurate):
@implementation Tview
-(id) initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"in initWithCoder");
if((self = [super initWithCoder:aDecoder])) {
[self setUpView];
}
return self;
}
-(void)awakeFromNib
{
NSLog(@"in awakeFromNib");
[self setUpView];
}
-(void)setUpView
{
NSLog(@"I am in setUpView");
NSArray *subviewArray=[[NSBundle mainBundle] loadNibNamed:@"tnib" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];
}
@end
In my ViewController.xib, I drag out a UIView and set the custom class name to Tview. I #import the Tview.h file and drag from the ViewController.xib to Tview.h and create following property:
@property (strong, nonatomic) IBOutlet Tview *myTview;
I build and run and get an infinite loop of hte following:
2013-03-04 06:49:05.452 Nibtest2[44524:11303] in initWithCoder
2013-03-04 06:49:05.455 Nibtest2[44524:11303] I am in setUpView
2013-03-04 06:49:05.456 Nibtest2[44524:11303] in initWithCoder
2013-03-04 06:49:05.458 Nibtest2[44524:11303] I am in setUpView
2013-03-04 06:49:05.459 Nibtest2[44524:11303] in initWithCoder
2013-03-04 06:49:05.460 Nibtest2[44524:11303] I am in setUpView
until it eventually crashes.
What am I doing wrong here?
thx in advance
initWithCoder:
, assuming that it is in your code and not done through the storyboard. – Sergey Kalinichenko[[Tview alloc] initWithCoder:...]
in the chain of calls. Is it done automatically through the story board, or you call it explicitly somewhere in out code? Perhaps there's another place in your code where you callloadNibNamed:@"tnib"
?.. – Sergey Kalinichenkotnib
referenceTview
? I would remove the calls tosetUpView
frominitWithCoder
andawakeFromNib
and then overrideviewDidLoad
. CallsetUpView
fromviewDidLoad
. – rmaddy