iOS12 使用 StoryBoard 显示模态 ViewController
初学iOS开发,想实现弹出模态窗口的效果。网上找了一些资料,发现要么年代久远,要么语焉不详。遂决定记录下详细步骤,以便后来者。
一、 要实现的功能如下:
1、有两个ViewController,命名为A-ViewController(简称A),B-ViewController(简称B)
2、A中放置一Button,点击后以模态方式打开B。
3、B中放置一Button,点击后通知A关闭B
二、 相关设置
1、按下图添加好2个ViewController,从A中的“打开模态视图”按钮拖动Segue到B。
2、将B中View的背景色设置为“Clear Color”或者自定义透明度为50%的颜色
3、选中 Segue,在Attributes inspector中,自定义Identifier,选择Kind为“Present Modally”,Presentation为“Over Current Context”或者“Over Full Screen”。
到此即可实现点击A中Button后以模态方式打开B。
三、 以Delegate方式通知A关闭B
1、在B中定义好protocol,添加delegate属性,点击按钮后触发delegate
2、在A中实现delegate,并在prepare(for segue)方法中订阅B的delegate。
四、 以unwind segue方式在A中关闭B
1、在A中添加unwind action
2、在B中添加一个按钮,按住Ctrl键,鼠标拖动到顶部的Exit图标,
3、点选A中添加的 unwind action 方法
五、 参考资料
2、https://useyourloaf.com/blog/unwind-segues-as-an-alternative-to-delegation/