-->

TransitionFromView removes previous view

2020-08-26 03:30发布

问题:

I am having problems using TransitionfromView while transitioning between views in my app.

Setup

This is the basic setup of the View Controller. It has two views in it. A MKMapView and a UITableView. When the toggle button is pressed, it is supposed to alternate views between map and table.

This is my *.h file

@interface BrowseBeaconsViewController : UIViewController <UITableViewDelegate, MKMapViewDelegate, UITableViewDataSource, CLLocationManagerDelegate >
{

__weak IBOutlet UIBarButtonItem *refreshBeacons;
__weak IBOutlet UIBarButtonItem *toggleView;
MKMapView* beaconMapView;
__weak IBOutlet UITableView* beaconTableView;
}

So the tableview comes from the storyboard while mapview is created in the program.

Problem

[UIView transitionFromView:beaconTableView toView:beaconMapView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {}];

When I transition from TableView from MapView the value of the tableview is null(0x0000000). I do understand the behaviour of the transitionfromview is to remove the view from the parent view. But when I try add the tableview as a subview after the transition it doesn't work, since the value is null. So my question is how do I add the tableview after the transition if the view is nulled?

PS: I apologize if this is a simple question, but I am new to iOS programming and did try to look in the forums before posting this question.

回答1:

From the docs on that method:

"By default, the view in fromView is replaced in the view hierarchy by the view in toView. If both views are already part of your view hierarchy, you can include the UIViewAnimationOptionShowHideTransitionViews option in the options parameter to simply hide or show them."

So, if you want both views to remain, add the beaconMapView to the view hierarchy, and include the UIViewAnimationOptionShowHideTransitionViews option.



回答2:

You need to keep a separate reference to beaconTableView or simply declare it as strong instead of weak. Since beaconTableView has been declared as weak, iOS 5+ understands that you don't need it hanging around once all other references to it have been removed, in this case by removing it from its parent view.



回答3:

Remove weak, otherwise the view gets released as soon as it looks no longer needed.