简体   繁体   中英

Cant load xib file from UIViewController

I am trying to load a xib file from my UIViewController .

In .h

UIView *myView;

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

In my .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. I have used the same code in the above cases, and it worked. Its one particular xib file that seems to have the problem. 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

Before loading this xib file I use the same code to load another xib file. And that works fine. I am trying to load the current xib file above this view.

Maybe make sure that your xib file has been added to the Copy Bundle Resources section of the target, in the Build Phases section. (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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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