调用performSeguewithIdentifier不会调用shouldperformseguewithIdentifier [英] Calling performSeguewithIdentifier doesn't call shouldperformseguewithIdentifier

查看:152
本文介绍了调用performSeguewithIdentifier不会调用shouldperformseguewithIdentifier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器。在视图controller1上我有以下内容:

I have two view controllers. On view controller1 I have the following:


  • 一个将我带到viewcontroller2的segue - 这个segue被命名为showme并附加到viewcontroller

  • 一个UIButton的IBAction

在我的代码中我有以下按钮按动作

In my code I have the following for the button press action

@IBAction func buttonPress(sender: AnyObject) {
    println("button pressed")
        performSegueWithIdentifier("showme", sender: self)
}

我还有以下内容方法:

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
    println("Should performing....")
    return true
}   

由于某种原因,永远不会调用shouldPerformSegueWithIdentifier函数。但是,如果我直接在UIButton上将segue添加到ViewController2中。

For some reason the shouldPerformSegueWithIdentifier function is never called. If however, I add the segue directly on the UIButton to ViewController2 it is.

我已经确认在我的按钮操作中调用它的方向是有效的(见下文),但这不是我理解的工作方式。 prepareforSegue也是如此..

I have confirmed that calling it direction within my button action works (see below), but this is not what I understand to be the way it works. The same is true for prepareforSegue..

@IBAction func buttonPress(sender: AnyObject) {
    println("button pressed")
    if (shouldPerformSegueWithIdentifier("showme", sender: self)){
        performSegueWithIdentifier("showme", sender: self)}
} 


推荐答案

这种行为非常自然,原因如下:

This behaviour is perfectly natural, for the following reasons:

1) shouldPerformSegueWithIdentifier 用于确保触发已在 Storyboards 中设置的segue,因此它仅在 Storyboard Segues 的情况下被调用,并且让您有机会实际执行segue。

1) shouldPerformSegueWithIdentifier is used to make sure that a segue that has been set up in Storyboards should be triggered, so it only gets called in the case of Storyboard Segues and gives you the chance to not actually perform the segue.

2)当你自己打电话给 performSegueWithIdentifier 时, shouldPerformSegueWithIdentifier 之所以打电话是因为可以假设您知道自己在做什么。调用 performSegueWithIdentifier 是没有意义的,但是从 shouldPerformSegueWithIdentifier NO c $ c>。

2) When you call performSegueWithIdentifier yourself, shouldPerformSegueWithIdentifier is not called because it can be assumed that you know what you are doing. There would be no point in calling performSegueWithIdentifier but then return a NO from shouldPerformSegueWithIdentifier.

这篇关于调用performSeguewithIdentifier不会调用shouldperformseguewithIdentifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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