0
votes

I have a problem in my app. When I pass a UIView to a SecondViewController in this way

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

(second.currentView is my property in SecondViewController and I pass to it my view)

At this point, it's all ok and my SecondViewController show correctly my currentView, but when I retund in FirstViewController my currentView disappear!!! Why? Can you help me? thanks

UPDATE

in FirstViewController.h

IBOutlet UIView *currentView;

in FirstViewController.m

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

in SecondViewController.h

@property (nonatomic, strong) UIView *currentView;

in 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);
}

this is my code in the two ViewControllers, and when I return in FirstViewController currentView there isn't... it disappear...

2
yes I update my questioncyclingIsBetter
yes, I use dismissViewControllercyclingIsBetter
I think that the best solution is to use a global var in appDelegate and in this way I solve all my problemscyclingIsBetter
I tried, it don't solve my problemcyclingIsBetter
it's simply [self dismissViewControllerAnimated:NO completion:nil]; inside an IBActioncyclingIsBetter

2 Answers

0
votes

A view only have super view, so when you add the view to SecondViewController you care adding it to its view hierarchy and thus removing to from your current view controller. When you call something like:

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

Thus if you want to add it back in the first view you need to add it again to view hierarchy of the first view.

A beter solution would be pass the data to the next view controller, not to view but the model. Like pass the model object holding the data presented in the view.

-1
votes

I solved my issue!!!

When I return in FirtsViewController I must add a new time my view in my self.view at last position, in this way:

[self.view addSubview:currentView];

[currentView setCenter:initially_center_view];

in this way I see my view!!!