0

我的应用程序有问题。当我以这种方式将 UIView 传递给 SecondViewController

SecondViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"secondviewcontroller"];
second.currentView = currentView;

(second.currentView 是我在 SecondViewController 中的属性,我将视图传递给它)

此时,一切正常,我的 SecondViewController 正确显示了我的 currentView,但是当我在 FirstViewController 中返回时,我的 currentView 消失了!!!为什么?你能帮助我吗?谢谢

更新

在 FirstViewController.h

IBOutlet UIView *currentView;

在 FirstViewController.m

SecondViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"secondviewcontroller"];
second.currentView = currentView;
[self presentViewController:second animated:NO completion:nil];

在 SecondViewController.h

@property (nonatomic, strong) UIView *currentView;

在 SecondViewController.m

- (void) viewDidAppear:(BOOL)animated{
[self.view addSubview:self.currentView];
self.currentView.center = CGPointMake(self.view.bounds.size.width/2,   self.view.bounds.size.height/2);
}

这是我在两个 ViewControllers 中的代码,当我在 FirstViewController currentView 中返回时没有......它消失了......

4

2 回答 2

0

视图只有超级视图,因此当您将视图添加到SecondViewController您的视图时,您需要将其添加到其视图层次结构中,从而从当前视图控制器中删除。当你调用类似的东西时:

- (void) viewDidLoad{
    [super viewDidLoad];
    // Here the view is removed the first view and placed on the second view.
    [self.view addSubView:self.secondView];
}

因此,如果要将其添加回第一个视图中,则需要再次将其添加到第一个视图的视图层次结构中。

更好的解决方案是将数据传递给下一个视图控制器,而不是视图而是模型。就像传递包含视图中呈现的数据的模型对象一样。

于 2013-11-04T15:17:43.267 回答
-1

我解决了我的问题!!!

当我返回 FirtsViewController 时,我必须在最后一个位置的 self.view 中添加一个新的时间视图,以这种方式:

[self.view addSubview:currentView];

[currentView setCenter:initially_center_view];

这样我就看到了我的看法!!!

于 2013-11-05T09:57:44.673 回答