非常好用的 unwind segue

Create Unwind Segues in Swift 3

This blog post is about how to create unwind segues in Swift. It’s super simple to implement them — so of course, this post will be short. (Source code: https://github.com/mimicatcodes/unwindSegueExample )

But wait, what are the unwind segues?

Let’s say we have three screens — 1,2 and 3. We want to move from 1 to 2 by creating a Show segue and now move to 3 from 2 by using a Present Modally segue in the Storyboard.

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="https://cdn-images-1.medium.com/max/800/1*DB4eAIZ7h8q9GvuGVMnFRw.png">

We just created a Present Modally segue from 2 and 3— this means if we want to go back to 2 from 3, we can simply dismiss the present view controller(VC3) by using this method:

@IBAction func dismissVC(_ sender: Any) {
     dismiss(animated: true, completion: nil)
}

Familiar, isn’t it? But what if we want to go back to 1, not 2, from 3?

This is when unwind segues come in handy.

With unwind segues, you can now travel back to 1 from 3 without a need to go back to 2 first and then to 1 through the navigation controller.

Follow these simple four steps to create Unwind segues:

  1. In the view controller you are trying to go back to, VC1 in my example, write this code:
@IBAction func unwindToVC1(segue:UIStoryboardSegue) { }

( Remember: It’s important to insert this method in the view controller you are trying to go back TO! )

2. In the Storyboard, go to the screen you’re trying to unwind from ( 3 in our case ), and control + drag the view controller icon to the Exit icon located on top.

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="https://cdn-images-1.medium.com/max/800/1*Q4OJNyWaiytSFVsQ7WsU8Q.png">

As seen above, when you are presented with IBAction option(s) to connect to, select the unwind segue action you just created in VC1.

3. Go to the document outline of the selected view controller in the Storyboard, select the unwind segue as shown below.

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="https://cdn-images-1.medium.com/max/800/1*mLIN-04vjjPq7N0IXzlSXQ.png">
1. Select the Unwind segue in the document outline

Now, go to the Attributes Inspector in the Utilities Pane and name the identifier of the unwind segue.

<img class="progressiveMedia-noscript js-progressiveMedia-inner" src="https://cdn-images-1.medium.com/max/800/1*8KalrmczmxN3ZlX0KvHCfw.png">
2. Specify the identifier of the Unwind segue

4. Finally, write this code where you want the unwind segue action to be triggered, V3 in our case.

@IBAction func goBackToOneButtonTapped(_ sender: Any) {
     performSegue(withIdentifier: "unwindSegueToVC1", sender: self)
}

The source file for this tutorial can be found here: https://github.com/mimicatcodes/unwindSegueExample

Happy Coding!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值