infinite loop trying to create a Custom View from

2019-02-14 14:31发布

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

标签: ios uiview ios6
2条回答
\"骚年 ilove
2楼-- · 2019-02-14 14:40

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.

查看更多
迷人小祖宗
3楼-- · 2019-02-14 14:41

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.

查看更多
登录 后发表回答