4
votes

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:

  1. Created a new Custom UIView via File -> New -> Objective-C Class called Tview
  2. Created a new view via File -> New -> User Interface -> View and called it tnib
  3. 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

2
I don't know where to start. I'm leaving it...trojanfoe
Please show the first call to initWithCoder:, assuming that it is in your code and not done through the storyboard.Sergey Kalinichenko
I mean, something in your code makes a call to the first [[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 call loadNibNamed:@"tnib"?..Sergey Kalinichenko
Does the tnib reference Tview? I would remove the calls to setUpView from initWithCoder and awakeFromNib and then override viewDidLoad. Call setUpView from viewDidLoad.rmaddy
@dasblinkenlight - I am loading it in the main ViewController by dragging it out from the Object Inspector and updating the Class Name in IBtimpone

2 Answers

4
votes

Your code sets up an infinite recursion: initWithCoder: calls setUpView, which instantiates a new UIView from a bundle, thus calling initWithCoder: 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 the awakeFromNib and the initWithCoder:. 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 of initWithCoder:. The only line that you may need is this:

[self addSubview:mainView];

However, it shouldn't be in the Tview's code: it needs to be moved to the parent controller that has the myTview property, and it should be modified to

[self.view addSubview:_myTview];

Add this line to viewDidLoad. It should take care of the problem.

0
votes

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:

myAccessoryView = NSBundle.mainBundle().loadNibNamed("MyAccessoryView", owner: self, options: nil)[0] as! MyAccessoryView

Don't load the view in a loadView() method! The loadView method serves for loading the view for your custom ViewController.