问题
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