Need assistance regarding transitionFromView:toVie

2020-02-15 15:24发布

问题:

Regarding transitionFromView:toView:duration:options:completion: Apple doc says this in last few lines:

This method modifies the views in their view hierarchy only. It does not modify your application’s view controllers in any way. For example, if you use this method to change the root view displayed by a view controller, it is your responsibility to update the view controller appropriately to handle the change.

If a ViewController has 2 full screen size views display one at a time then no issues:

[transitionFromView:self.view toView:self.view2...

but what this means it is your responsibility to update the view controller appropriately to handle the change?

if I do this:

secondViewController *sVc = [[secondViewController alloc]init];
[transitionFromView:self.view toView:sVc.view...

how its my responsibility to update the view controller appropriately to handle the change? or how to update ViewController?

UPDATE

I created a single view projec, add secondVC then in firstVC on button tap i did this:

self.svc = [[secondVC alloc]init]; 

[UIView transitionFromView:self.view toView:self.svc.view duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {}];

... secondVC viewDidLoad is working its nslog is working.

Then how to handle updating of viewcontroller?

回答1:

The statement "it is your responsibility to update the view controller appropriately to handle the change." it meant that you have to appropriately call view hierarchy delegate methods such as:

- (void)viewDidLoad;
- (void)viewDidUnload;
- (void)viewWillAppear;
- (void)viewDidDisappear;

And other methods that are responsible for proper view management.

Here are some examples.



回答2:

When we use transitionFromView:toView:duration:options:completion: we are only bringing toView up in view hierarchy. but Apple says we should handle updating of ViewControllers which are parent of these views.

Maintaining viewcontroller in navigationcontroller stack...

For .ex: if You have TabController in your application,

somewhere at tabIndex two you required to show view of viewcontroller at tabindex 1, then you should update your tabIndex when you will use transitionfromview method

[UIView transitionFromView:fromView 
                        toView:toView 
                      duration:0.5 
                       options:(controllerIndex > tabBarController.selectedIndex ? UIViewAnimationOptionTransitionCurlUp : UIViewAnimationOptionTransitionCurlDown)
                    completion:^(BOOL finished) {
                        if (finished) {
                            tabBarController.selectedIndex = controllerIndex;
                        }
                    }];