0

这听起来像是一个简单的问题,但我在任何地方都找不到答案。

我在视图之间切换,一切都很好。我可以在视图之间传递变量和数据,但是当我想回到前一个视图时就会出现问题。

视图上的任何数据都消失并被删除,就好像它没有通过一样。当然,我不想在切换到新视图时卸载数据。这可能吗?

这就是我在项目中的视图之间切换的方式。有没有更好的办法?

TicketStart *backPage=[[TicketStart alloc]initWithNibName:@"TicketStart" bundle:nil];
[self addChildViewController:backPage];
[self.view addSubview:backPage.view];

当我搜索这个问题时,我能找到的只是关于传递事物或加载新视图的问题。抱歉,如果有人问过这个问题,但我在任何地方都找不到答案。

4

1 回答 1

0

我不确定是什么触发了您的视图更改,因此您必须编写该代码。

家长

@implementation ParentViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.vcOne = [[OneViewController alloc] init];
    [self addChildViewController:self.vcOne];
    [self.view addSubView:self.vcOne.view];
    self.vcTwo = [[TwoViewController alloc] init];
}

-(void)switchViews {
    //logic to animate in view that isn't present
}
@end

查看控制器一

@interface OneViewController ()
@property(strong, nonatomic) UIView *vOne;
@end

@implementation OneViewController
-(UIView*)vOne {
    if(_vOne == nil) {
        _vOne = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
    }
    return _vOne;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubView:self.vOne];
}
@end

视图控制器二

@interface TwoViewController ()
@property(strong, nonatomic) UIView *vTwo;
@end
@implementation TwoViewController
-(UIView*)vTwo {
    if(_vTwo == nil) {
        _vTwo = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
    }
    return _vTwo;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubView:self.vTwo];
}
@end
于 2013-07-26T20:39:42.230 回答