尝试从IB中的xib创建自定义视图的无限循环 [英] infinite loop trying to create a Custom View from xib in IB

查看:115
本文介绍了尝试从IB中的xib创建自定义视图的无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个问题的后续行动(使用IB / Xcode 4.5.1和iOS模拟器6.0定制UIView操作方法)但不需要阅读 - 这是我第一次尝试创建自定义UIView和显而易见的是,我提前得到的任何帮助都没有提到。

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.

我有一个从Interface Builder派生的自定义UIView。我想将设置大小设置为200w x 200h,将backgroundColor设置为Green。当我创建时,我做了以下操作:

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. 通过文件创建一个新的自定义UIView - >新建 - > Objective-C类称为Tview

  2. 通过文件创建一个新视图 - >新建 - >用户界面 - >查看并在Canvas中调用它tnib

  3. ,我删除了视图和从对象检查器添加了一个新视图,并将类名设置为Tview。此外,在属性检查器中,我将大小设置为自由形式,将背景颜色设置为绿色。在Size Inspector中,我将宽度设置为200,将高度设置为200.

在我的Tview.m中,我设置了以下(我已根据其他SO问题进行了一些更新,但目前尚不清楚这些是否仍然是当前或准确的):

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

在我的ViewController.xib中,我拖出一个UIView并将自定义类名设置为Tview。我#import Tview.h文件并从ViewController.xib拖到Tview.h并创建以下属性:

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;

我构建并运行并获得无限循环的hte:

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

直到它最终崩溃。

我在这里做错了什么?

提前thx

推荐答案

您的代码设置无限递归: initWithCoder:调用 setUpView ,它从一个包中实例化一个新的 UIView ,从而间接调用 initWithCoder:并完成循环。

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.

在读取相同NIB时,不应该在调用的代码中访问NIB。您应该在包含对象的代码中读取NIB,而不是在 Tview 对象本身的代码中。否则,你会得到一个无限循环。

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.

你需要完全远程调用 setUpView 以及来自 awakeFromNib initWithCoder:。通过从对象检查器拖出来在界面构建器中建立的连接似乎已经导致NIB文件正确加载,这可以通过调用 initWithCoder:来证明。您可能需要的唯一一行是:

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];

但是,它不应该在 Tview 代码:需要将其移动到具有 myTview 属性的父控制器,并且应该将其修改为

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];

将此行添加到 viewDidLoad 。它应该解决这个问题。

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

这篇关于尝试从IB中的xib创建自定义视图的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆