放松segue动作之前该怎么做? [英] How to do something before unwind segue action?

查看:143
本文介绍了放松segue动作之前该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚弄清了什么是解散segue以及如何将其与

I've just figured out what is an unwind segue and how to use it with the great answers from this question. Thanks a lot.

但是,这是一个像这样的问题:

However, here's a question like this:

比方说,场景B中有一个按钮可以将场景展开到场景A,在场景发生之前,我希望先完成一些操作,例如将数据保存到数据库中.我在B.swift中为此按钮创建了一个动作,但似乎它直接进入场景A,而没有采取指定的动作.

Let's say there is a button in scene B which unwind segues to scene A, and before it segues I want something to be done, such as saving the data to database. I created an action for this button in B.swift, but it seems it goes directly to scene A without taking the action appointed.

任何人都知道为什么或如何做吗?

Anyone knows why or how to do this?

谢谢.

推荐答案

您可以按照描述的方式进行操作,也可以通过在viewController中使用prepareForSegue覆盖函数来展开:

You can do it the way you describe, or by using a prepareForSegue override function in the viewController you are unwinding from:

@IBAction func actionForUnwindButton(sender: AnyObject) {
    println("actionForUnwindButton");
}

或...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    println("prepareForSegue");
}

第一个示例如您所描述.该按钮已连接至展开界面和Interface Builder中的按钮动作.按钮动作将在segue动作之前触发.也许您没有将操作连接到界面生成器中的按钮上?

The first example is as you describe. The button is wired up to the unwind segue and to the button action in Interface Builder. The button action will be triggered before the segue action. Perhaps you didn't connect the action to the button in interface builder?

第二个示例使您可以访问segue的sourceViewControllerdestinationViewController(如果它们也很有用)(您也可以在目标视图控制器的unwind segue的功能中获得它们).

The second example gives you have access to the segue's sourceViewController and destinationViewController in case that is also useful (you also get these in the unwind segue's function in the destination view controller).

如果您想推迟展开按钮直到按钮的本地操作完成,您可以使用self.performSegueWithIdentifier(或遵循wrUS61的建议)直接从按钮操作中调用按钮(而不是将其挂接到情节提要中)

If you want to delay the unwind segue until the button's local action is complete, you can invoke the segue directly from the button action (instead of hooking it up in the storyboard) using self.performSegueWithIdentifier (or follow wrUS61's suggestion)

编辑

您似乎有些疑问,是否可以通过将按钮连接到放松按钮和按钮动作上来进行操作.我已经建立了一个像这样的小测试项目:

you seem to have some doubts whether you can work this by wiring up your button both to an unwind segue and to a button action. I have set up a little test project like this:

class BlueViewController: UIViewController {

    @IBAction func actionForUnwindButton(sender: AnyObject) {
        println("actionForUnwindButton");
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        println("prepareForSegue");
    }
}


class RedViewController: UIViewController {

    @IBAction func unwindToRed(sender: UIStoryboardSegue) {
        println("unwindToRed");
    }
}

BlueViewController具有一个在情节提要中连接到unwindToRed展开键和actionForUnwindButton按钮的按钮.它还会覆盖prepareForSegue,因此我们可以记录播放顺序.

BlueViewController has a button that is connected in the storyboard to BOTH the unwindToRed unwind segue AND the actionForUnwindButton button. It also overrides prepareForSegue so we can log the order of play.

输出:

actionForUnwindButton
prepareForSegue
unwindToRed

故事板:

编辑2

您的演示项目显示此正常工作.不同之处在于您使用的是barButtonItem来触发操作,而我使用的是常规按钮. barButtonItem失败,而常规按钮成功.我怀疑,这是由于消息传递顺序的差异(以下是推测,但符合观察到的行为):

your demo project shows this not working. The difference is that you are using a barButtonItem to trigger the action, whereas I am using a regular button. A barButtonItem fails, whereas a regular button succeeds. I suspect that this is due to differences in the order of message passing (what follows is conjecture, but fits with the observed behaviour):

(A)视图控制器中的UIButton

(A) UIButton in View Controller

ViewController的按钮接收touchupInside
-(1)向其方法发送操作
-(2)将segue放开动作发送到情节提要segue
收到的所有消息以及按此顺序执行的方法:

ViewController's button receives touchupInside
- (1) sends action to it's method
- (2) sends segue unwind action to storyboard segue
all messages received, and methods executed in this order:

actionForUnwindButton
prepareForSegue
unwindToRed

(B)导航控制器工具栏中的UIBarButtonItem

(B) UIBarButtonItem in Navigation Controller Toolbar

工具栏上的buttonItem接收touchupInside
-(1)将情节放松动作发送到情节提要剧情
-(2)(可能的话)将动作发送到viewController的方法

Tool bar buttonItem receives touchupInside
- (1) sends segue unwind action to storyboard segue
- (2) (possibly, then) sends action to viewController's method

执行顺序为

prepareForSegue
unwindToRed
actionForUnwindButton

收到

prepareForSegueunwind消息.但是actionForUnwindButton消息被发送到nil,因为在segue期间viewController被破坏了.因此它不会执行,并且日志显示

prepareForSegue and unwind messages received. However actionForUnwindButton message is sent to nil as viewController is destroyed during the segue. So it doesn't get executed, and the log prints

prepareForSegue
unwindToRed

对于(B),viewController在方法到达之前就被销毁,因此不会被触发

In the case of (B), the viewController is destroyed before the method reaches it, so does not get triggered

所以看来您的选择是...
(a)使用带有操作的UIButton并展开segue
(b)使用prepareForSegue触发您的操作,该操作将在viewController仍处于活动状态时以及在发生segue之前触发.
(c)不要使用放松动作,只需使用按钮动作即可.在动作方法中,您可以通过在导航控制器上调用popToViewController来放松".

So it seems your options are...
(a) use a UIButton with action and unwind segue
(b) trigger your actions using prepareForSegue, which will be triggered while the viewController is still alive, and before the segue takes place.
(c) don't use an unwind segue, just use a button action. In the action method you can 'unwind' by calling popToViewController on your navigation controller.

顺便说一句,如果您在viewController上实现一个ToolBar(不使用导航控制器的工具栏),结果是相同的:segue首先被触发,因此按钮操作失败.

By the way, if you implement a toolBar on the viewController (not using the navigation controller's toolbar) the result is the same: segue gets triggered first, so button action fails.

这篇关于放松segue动作之前该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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