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
Actually my resolution to this problem was, to load the view in a viewDidLoad in my CustonViewController where I wanted to use the view like that:
Don't load the view in a
loadView()
method! The loadView method serves for loading the view for your custom ViewController.Your code sets up an infinite recursion:
initWithCoder:
callssetUpView
, which instantiates a newUIView
from a bundle, thus callinginitWithCoder:
indirectly, and completing the cycle.You should not access the NIB in the code that is called when the same NIB is read. You should read the NIB in the code of the containing object, not in the code of the
Tview
object itself. Otherwise, you get an infinite cycle.You need to remote the
setUpView
altogether, along with its calls from theawakeFromNib
and theinitWithCoder:
. It appears that the connection that you made in the interface builder by dragging out from object inspector is already causing the NIB file to load correctly, as evidenced by the call ofinitWithCoder:
. The only line that you may need is this:However, it shouldn't be in the
Tview
's code: it needs to be moved to the parent controller that has themyTview
property, and it should be modified toAdd this line to
viewDidLoad
. It should take care of the problem.