Tuesday, May 5, 2015

Using hidden button to trigger segue action for ios screen navigation

When handling ios screen navigation in storyboard, segue is very easy to define how the navigation and unwinding happens. However, navigation with segue requires an UI element (such as a button or a tableviewCell item) on the view so we can drag and drop from it to the destination viewcontroller, and the issue is, quite often there is not an element in UI to trigger the navigation, and the navigation is triggered by other logic conditions.

In order to still use the segue to configure the navigation and unwinding, an easy way is creating a hidden button in the source viewcontroller, and then creating a segue by dragging and dropping from the hidden button to the destination viewcontroller. Then in the code, when the navigation is required, just programmatically call button onclicked event or performSegueWithIdentity method to start the navigation.

The similar way can be used to unwind to the previous viewcontroller, just creating a hidden button on the current view controller and then associate it with the current viewcontroller's exit delegate, and then select an winding action method from the previous viewcontroller.

One benefit of this approach is easy to test, you can always show the hidden button and click it to see whether the screen navigation works. Once it works, then hiding it and programmatically trigger the segue based on the application logic.

No comments:

Post a Comment