停止视图卸载其数据

时间:2013-07-26 20:27:51

标签: ios uiviewcontroller

这可能听起来像一个简单的问题,但我无法在任何地方找到答案。

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

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

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

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

当我搜索这个问题时,我发现所有关于传递或加载新视图的问题。很抱歉,如果有人提出这个问题,但我无法在任何地方找到答案。

1 个答案:

答案 0 :(得分: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

查看Controller One

@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