ios 当我在不使用 performSegueWithIdentifier 的情况下单击按钮时,prepareForSegue 不会被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21619139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:22:47  来源:igfitidea点击:

prepareForSegue is not getting called when I click on a button without using performSegueWithIdentifier

iosiphoneobjective-csegueuistoryboardsegue

提问by Amro Younes

Based on the Stanford iOS course I am playing with modal view controllers. In the demo they have a button that would launch a modal view and when it is clicked the function prepareForSegue is called. I mimicked the code and implementation into my project with the only difference is that my demo is on an iPhone storyboard and theirs is on the iPad.

基于斯坦福 iOS 课程,我正在使用模态视图控制器。在演示中,他们有一个按钮可以启动一个模态视图,当它被点击时,prepareForSegue 函数被调用。我将代码和实现模仿到我的项目中,唯一的区别是我的演示在 iPhone 故事板上,他们在 iPad 上。

I noticed that while my modal view controller is coming up, it does not call prepareForSegue prior to that. I searched the Stanford project to see where they may register any segue behavior before prepareForSegue is called but there is no evidence. Can anyone shed some light on this. I searched stack overflow and all I found were that users were missing the call implementation of performSegueWithIdentifier. However, in the Stanford demo they never do that.

我注意到当我的模态视图控制器出现时,它没有在此之前调用 prepareForSegue。我搜索了斯坦福项目,看看在调用 prepareForSegue 之前他们可以在哪里注册任何 segue 行为,但没有证据。任何人都可以对此有所了解。我搜索了堆栈溢出,我发现所有用户都缺少 performSegueWithIdentifier 的调用实现。然而,在斯坦福演示中,他们从未这样做过。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier hasPrefix:@"Create Label"]) {
        AskerViewController *asker = (AskerViewController *)segue.destinationViewController;
        asker.question = @"What do you want your label to say?";
        asker.answer = @"Label Text";
        asker.delegate = self;
    }

}

Here is an example of there storyboard: enter image description here

这是故事板的示例: 在此处输入图片说明

Here is an example of my storyboard: enter image description here

这是我的故事板的一个例子: 在此处输入图片说明

In the debugger when I stop in the Stanford Demo code the call stack shows that the storyboard is performing a segue action, what do I need to configure in my storyboard to achieve the same result? enter image description here

在调试器中,当我停在斯坦福演示代码中时,调用堆栈显示故事板正在执行转场动作,我需要在故事板中配置什么才能达到相同的结果? 在此处输入图片说明

回答by Amro Younes

Well, as it turns out, my view controller where button calls the modal view did not have the right class where prepareForSegue is implemented. It was the default UIViewController instead of my custom class.

好吧,事实证明,我的按钮调用模态视图的视图控制器没有实现 prepareForSegue 的正确类。它是默认的 UIViewController 而不是我的自定义类。

The way I figured it out was by putting a break point in viewDidLoad and even that was not breaking and thus I suspected that in the storyboard I did not have the right class associated with the view where the button is implemented.

我想出来的方法是在 viewDidLoad 中放置一个断点,即使它也没有中断,因此我怀疑在故事板中我没有与实现按钮的视图相关联的正确类。

回答by Manuel BM

For others with this problem, if you are now using Swift 3 having the following function will not throw errors as it is the correct syntax but it will not work because it is the Swift 2 function:

对于有此问题的其他人,如果您现在使用 Swift 3,具有以下函数不会抛出错误,因为它是正确的语法,但它不会工作,因为它是 Swift 2 函数:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // code
}

You must update to Swift 3 "prepare" function for it to work for segues:

您必须更新到 Swift 3“准备”功能才能使其适用于 segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // code
}

回答by DonVaughn

When hooking up an automatic segue for a table view, there is, in addition to Amro's answer (not assigning your corresponding subclass), another two cases where prepareForSeguemight not be called. Ensure you've:

在为表视图连接自动转场时,除了 Amro 的答案(不分配您的相应子类)之外,还有另外两种prepareForSegue可能不会被调用的情况。确保您:

  1. hooked up the segue from the table view prototype cell, notthe table view controller.

  2. used a segue from under the "Selection Segue" group in the segue connection pop-up menu, notone under "Accessory Action".

  1. 从表视图原型单元而不是表视图控制器中连接了 segue 。

  2. 使用了 segue 连接弹出菜单中“Selection Segue”组下的 segue,而不是“Accessory Action”下的 segue 。

Hooking up automatic segues

连接自动转场

[Click image to enlarge]

[点击图片放大]

回答by bhavya kothari

Whether its an Modal or Push Segue below code will always be called

无论是 Modal 还是 Push Segue 下面的代码都将始终被调用

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Create Label"]) {
        SignUpViewController *asker = segue.destinationViewController;
    }
}

回答by Mohit Joshi

I had a similar problem with UICollectionViewCellas the source of segue.
It seems that for the storyboard segue to work (without performSegueWithIdentifier) it's required that you have a custom subclass of UICollectionViewCell, and use it as a Classfor the CollectionViewCellon the story board.

我在UICollectionViewCell作为 segue 的来源时遇到了类似的问题。
看来,故事板顺着接下去的工作(不 performSegueWithIdentifier)它需要你有一个自定义子类UICollectionViewCell,并把它作为一个ClassCollectionViewCell上故事板。

回答by Honey

I had a similar issue.

我有一个类似的问题。

The thought process was:

思考过程是:

  1. make sure you have the correct method signature. It has changed in Swift 3.
  2. Then make sure the way you have hooked up the button (or whatever that triggers the segue) matches with the way you have hooked the segue in storyboard. Sometimes you call a button, but haven't properly hooked up the segue from that button to the destination viewcontroller.
  3. Be sure the identifier of the segue is correct. Though this isn't the reason the prepareForSegue doesn't get called, this only the reason that a specific segue isn't called.
  1. 确保你有正确的方法签名。它在 Swift 3 中发生了变化。
  2. 然后确保您连接按钮的方式(或任何触发转场的方式)与您在故事板中连接转场的方式相匹配。有时您调用一个按钮,但没有正确地将该按钮的 segue 连接到目标视图控制器。
  3. 确保 segue 的标识符是正确的。虽然这不是不调用 prepareForSegue 的原因,但这只是不调用特定 segue 的原因。

回答by Vincent Guyard

In my case, it ocured because my controller was extending another controller (Eureka Form View Controller = FormViewController) witch has implemented the performSegue function like this:

在我的情况下,它发生是因为我的控制器正在扩展另一个控制器(Eureka Form View Controller = FormViewController)女巫已经实现了这样的 performSegue 函数:

open override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // code
}

My function was implemented like this:

我的功能是这样实现的:

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // code
}

To solve this, i just added override before:

为了解决这个问题,我之前刚刚添加了覆盖:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // code
}

Voila!

瞧!

回答by Logan Sease

In my case, I did not set the module under the name of the class in the storyboard of the view controller that contains the segue. It was set to none and once I clicked in the module field it set to the correct project / module and resolved the issue.

在我的情况下,我没有在包含 segue 的视图控制器的故事板中的类的名称下设置模块。它被设置为无,一旦我点击模块字段,它就会设置为正确的项目/模块并解决了问题。

回答by ClayJ

In my case, I have a base class for several view controllers in my project. That base class has an implementation of prepareForSegue()which wasn't getting called in every case. The problem was that in one of my view controllers that inherits from the base class and overrides its prepareForSegue()implementation, I forgot to call super.prepareForSegue().

就我而言,我的项目中有多个视图控制器的基类。该基类的实现prepareForSegue()并非在每种情况下都被调用。问题是,在我从基类继承并覆盖其prepareForSegue()实现的视图控制器之一中,我忘记调用super.prepareForSegue().

回答by Nagarjun

Firstly you have to select on your button + ctrl drag item to you view controller choose selection segue .Later, you have to name segue identifier properly.

首先,您必须在按钮上选择 + ctrl 拖动项目到您的视图控制器选择选择 segue 。后来,您必须正确命名 segue 标识符。

Don't connect to one view controller to other view controller. import UIKit.framework for this Then this method will get called.

不要将一个视图控制器连接到另一个视图控制器。为此导入 UIKit.framework 然后将调用此方法。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"identifierName"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        ExampleViewController *destViewController = segue.destinationViewController;
    }
}