简体   繁体   English

无法从UIViewController加载xib文件

[英]Cant load xib file from UIViewController

I am trying to load a xib file from my UIViewController . 我正在尝试从我的UIViewController加载一个xib文件。

In .h 在.h

UIView *myView;

@property(nonatomic, retain) IBOutlet UIView *myView;

In my .m 在我的.m

@synthesize myView;

if([[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil]) { 
            [self.myView setFrame:CGRectMake(0, 0, 320, 480)];

            [[UIApplication sharedApplication].keyWindow addSubview:self.myView];

}

From the same UIViewController I load and remove xib files. 从相同的UIViewController我加载和删除xib文件。 I have used the same code in the above cases, and it worked. 我在上面的例子中使用了相同的代码,并且它有效。 Its one particular xib file that seems to have the problem. 它的一个特定的xib文件似乎有问题。 I have checked, whether the window is nil. 我检查过,窗口是否为零。 But its not. 但事实并非如此。 Cant figure out what the problem is. 无法弄清问题是什么。

Edit: Is it possible that self is getting released prematurely 编辑: self是否有可能过早地被释放

Before loading this xib file I use the same code to load another xib file. 在加载此xib文件之前,我使用相同的代码来加载另一个xib文件。 And that works fine. 这很好。 I am trying to load the current xib file above this view. 我试图在此视图上方加载当前的xib文件。

Maybe make sure that your xib file has been added to the Copy Bundle Resources section of the target, in the Build Phases section. 也许确保您的xib文件已添加到目标的“复制包资源”部分,在“构建阶段”部分中。 (And double check as well that it has been added to the Navigation menu resources). (并仔细检查它是否已添加到导航菜单资源)。

Try this: 尝试这个:

if([[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0]) { 
            [self.myView setFrame:CGRectMake(0, 0, 320, 480)];

            [[UIApplication sharedApplication].keyWindow addSubview:self.myView];

}   

I think the problem may be that "loadNibNamed" returns an NSArray of top-level objects. 我认为问题可能是“loadNibNamed”返回顶级对象的NSArray。

如果您将此视图加载到另一个UIView ,并且如果正确加载了该UIView ,则可以将此视图添加为UIView的子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM