如何解除在一个模式segue在UIStoryboard中提出的模态? [英] How to dismiss a modal that was presented in a UIStoryboard with a modal segue?

查看:105
本文介绍了如何解除在一个模式segue在UIStoryboard中提出的模态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:我有一个故事板设置,有两个简单的视图控制器A和B.在A中有一个按钮,转换到B与模态segue。 B在A的顶部显示模态转换。很好。



问题:有一种方法可以通过一些简单的故事板魔法?



请注意,如果这一切都在一个导航控制器中,并且我使用一个推动segue,它将被隐式地由导航控制器照顾。将有一个后退按钮。没有什么可比的模态,我需要自己构建UI,这是很好,但我想知道是否有一个segue机制,我可以用来发信号从B回到A。



现在,构建从B到A的oldskool方法是:




  • 在B上创建一个委托属性

  • 当模态转换segue播放时,将A设置为B的代理(我可以使用prepareForSegue:sender:在A的代码中加入)






有没有一些UIStoryboard机制我错过了,基本上会做一个反向模式segue?

/ p>

解决方案

没有任何故事板魔法来关闭模态视图控制器,而不写至少一点代码。 >

但是,你必须实现自己的一些代码,你不一定要去那么麻烦。你可以在视图控制器B中调用 [self dismissViewControllerAnimated:YES completion:nil] 的按钮。 (文档说,呈现视图控制器应该是关闭,但他们还表示,如果调用呈现视图控制器的消息将被转发到呈现视图控制器。如果你想更清楚它 - 需要在某些情况下,比如当一个模态视图控制器从另一个呈现时 - 您可以使用 self.presentingViewController 显式引用演示者并调用

您在某些应用程式中看到委托商家,因为这是通知检视控制器A的一种方式用户在视图控制器B ...但它不是唯一的方法。有KVO,通知,或者只是简单地调用A的方法引用它与 self.presentingViewController (假设B知道它总是由A提出)。如果A不需要知道B中发生了什么(比如说,因为用户点击了一个取消按钮),没有必要做任何事情 - 你可以只是关闭模态,并完成它。 p>




在iOS 6及更高版本中,展开segues会增加另一个选项,提供一些storyboard魔术 (或以其他方式退出序列序列)。但是这种方法仍然需要一些代码 - 你不能完全在故事板中设置它。另一方面,该代码提供了一个从正被关闭的视图控制器(B)到呈现它的(A)的信息的路径。



Apple有详细介绍了解决问题的技术说明,但这里是短版本:


  1. 在您想要的视图控制器类上定义 IBAction 解开 - 呈现模态视图控制器,而不是模态视图控制器本身(查看控制器A在你的问题)。与正常的 IBAction 方法不同,这些方法应该使用类型 UIStoryboardSegue * 的参数;例如

       - (IBAction)unwindToMainMenu:(UIStoryboardSegue *)sender 


  2. 在提供的视图控制器(问题中的B)中,将控件连接到绿色退出图标,然后选择您定义的方法。


  3. 在您的展开方法实施中,您可以参考segue的 sourceViewController 控制器正在关闭。您不需要调用 dismissViewControllerAnimated:completion:,因为segue会处理关闭消失的视图控制器。



    1. Setup: I have a storyboard set up, with two simple view controllers A and B. There is a button in A, that transitions to B with a modal segue. B is presented with a modal transition on top of A. It’s fine.

      Question: is there a way to pop B away and get back to A with some simple storyboard magic?

      Note that if this was all in a navigation controller, and I used a push segue, it would be implicitly be taken care of by navigation controller. There would be a "back" button. There’s nothing comparable for modals, I need to build the UI myself which is fine, but I am wondering if there is a segue mechanic I can use to signal to go back from B to A.

      Now the oldskool method to build going back from B to A would be:

      • create a delegate property on B
      • set A to be B's delegate when the modal transition segue plays back (I can hook into this using prepareForSegue:sender: in A’s code)
      • when it’s time to dismiss, B signals to its delegate
      • A implements a delegate method that dismisses B

      This works, but feels like too much overhead and silly.

      Is there some UIStoryboard mechanic that I have missed, that would basically do a "reverse modal segue"?

      解决方案

      There isn't any storyboard magic for dismissing a modal view controller without writing at least a little bit of code.

      But while you do have to implement some code of your own, you don't necessarily have to go to that much trouble. You can just have a button in view controller B that calls [self dismissViewControllerAnimated:YES completion:nil]. (The docs say the presenting view controller should be the one to dismiss, but they also say that the message will be forwarded to the presenting view controller if called on the presentee. If you want to be more explicit about it -- and you'll need to be in some cases, like when one modal view controller is presented from another -- you can explicitly reference the presenter with self.presentingViewController and call dismiss... from there.)

      You see the delegate business in some apps because it's one way of notifying view controller A about whatever the user did while in view controller B... but it's not the only way. There's KVO, notifications, or just plain calling A's methods after referencing it with self.presentingViewController (assuming B knows it's always getting presented by A). And if A doesn't need to know about what happened in B (say, because the user hit a Cancel button), there's no need to do any of that -- you can just dismiss the modal and be done with it.


      In iOS 6 and later, unwind segues add another option, providing a little bit of "storyboard magic" for dismissing modal view controllers (or otherwise "backing out" of a sequence of segues). But this approach still requires some code -- you can't set it up entirely in storyboard. On the plus side, though, that code provides a path for getting info from the view controller being dismissed (B) to the one that presented it (A).

      Apple has a tech note about unwind segues that covers them in detail, but here's the short version:

      1. Define an IBAction method on the view controller class you want to unwind to -- the one that presents a modal view controller, not the modal view controller itself (view controller A in your question). Unlike normal IBAction methods, these should take a parameter of type UIStoryboardSegue *; e.g.

        - (IBAction)unwindToMainMenu:(UIStoryboardSegue*)sender
        

      2. In the presented view controller (B in the question), wire a control to the green Exit icon, and choose the method you defined.

      3. In your unwind method implementation, you can refer to the segue's sourceViewController to retrieve information from the view controller being dismissed. You don't need to call dismissViewControllerAnimated:completion: because the segue handles dismissing the view controller that's going away.

      这篇关于如何解除在一个模式segue在UIStoryboard中提出的模态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆