Use destinationViewController button in a custom segue

南楼画角 提交于 2019-12-11 06:44:10

问题


My goal is to make a custom segue with a custom animation as follows: I want the segue to cover a button from the sourceViewController with a button from the destinationViewController with an effect similar to the navigation controller's push effect, i.e. the new button is supposed to push the old button away from right to left.

I have been able to make the old button (from the sourceViewController) move away as desired:

[UIView animateWithDuration:1.0 animations:^{
        // set the target frame for animated view
        sourceViewController.optionsButton.frame = leftButtonTargetFrame;
    } completion:^(BOOL finished) {
        [navigationController pushViewController:destinationViewController animated:NO];
        // reset the button's frame back to its original frame
        sourceViewController.optionsButton.frame = leftButtonInitFrame;
}];

But I am struggling to make the new button (from the destinationViewController) move in. The reason is that I cannot access the destinationViewController's view elements: While performing the segue they are not instantiated. And I cannot animate a button that is not instantiated.

So how can I replace a button in the sourceViewController with a button from the destinationViewController?


回答1:


The view of the destination view controller hasn't been initialized/loaded at the time when you try to access the buttons. To load the view of the destination view controller, you can simply access the view property. Do this before using the buttons: [destinationViewController view];

destinationViewController.view; would also work, but it would generate a compiler warning.

Background Information:

If you access the view property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view.

The method loadView loads the view that the controller manages. You should never call this method directly.




回答2:


You are correct that you cannot animate an object that does not yet exist. However, you can fake it.

  1. Create a place-holder button that will look identical to the button that will be in the new view controller.
  2. Animate it to the correct place.
  3. As the destination view controller comes in, its button should be invisible.
  4. After the the view controller is in place (i.e. the segue has finished) the destination view controller can ensure the proper placement if its button and make its actual button visible.

Hope this helps.



来源:https://stackoverflow.com/questions/15311630/use-destinationviewcontroller-button-in-a-custom-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!