iOS: Returning to main XIB from secondary XIB

回眸只為那壹抹淺笑 提交于 2019-12-11 06:28:40

问题


I am trying to change a back to the main xib after I was on a second XIB. This is the current code I am using:

NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"Results1" owner:self options:nil];
    UIView *aView = [nibObjs objectAtIndex:0];
    self.view = aView;

I have a button on the second xib that returns back to the originating xib. When I hit the button the app crashes. I am not sure why because it does not display an error - it just crashes. Here is the code on the button on the second xib or "Results1" xib file:

-(IBAction)Button100:(id)sender
{
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"main" owner:self options:nil];
UIView *aView = [nibObjs objectAtIndex:0];
self.view = aView;
}

Not sure what I am doing wrong.


回答1:


Add as subviews instead, so you can then just 'pop' the Results1 view off:

// in your .h
@property (strong) UIView *aView;

// replace your provided code with this
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"Results1" owner:self options:nil];
self.aView = [nibObjs objectAtIndex:0];
[self.view addSubview:self.aView];

- (IBAction)Button100:(id)sender
{
    [self.aView removeFromSuperview];
    self.aView = nil;
}

Just a suggestion — if you want them to 'swap', consider using the class method transitionFromView:toView:duration:options:completion: in UIView.




回答2:


The problem is I needed to turn off Automatic Reference Counting.



来源:https://stackoverflow.com/questions/11893963/ios-returning-to-main-xib-from-secondary-xib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!