当我不使用performSegueWithIdentifier点击一个按钮时,prepareForSegue不会被调用

时间:2021-08-17 21:16:20

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。我搜索了Stanford项目,看看在调用prepareForSegue之前,他们可以在哪里注册任何segue行为,但没有证据。有人能解释一下吗?我搜索了stack overflow,发现用户丢失了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: 当我不使用performSegueWithIdentifier点击一个按钮时,prepareForSegue不会被调用

这里有一个故事板的例子:

Here is an example of my storyboard: 当我不使用performSegueWithIdentifier点击一个按钮时,prepareForSegue不会被调用

这是我的故事板的一个例子:

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? 当我不使用performSegueWithIdentifier点击一个按钮时,prepareForSegue不会被调用

在调试器中,当我在斯坦福演示代码中停止时,调用堆栈显示故事板正在执行一个segue操作,我需要在我的故事板中配置什么来实现相同的结果?

10 个解决方案

#1


26  

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中放置断点,即使这样也没有中断,因此我怀疑在故事板中,我没有与实现按钮的视图相关联的正确类。

#2


9  

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 prepareForSegue might not be called. Ensure you've:

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

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

    连接表视图原型单元格中的segue,而不是表视图控制器。

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

    在segue连接弹出菜单的“选择segue”组下使用segue,而不是在“辅助动作”下。

当我不使用performSegueWithIdentifier点击一个按钮时,prepareForSegue不会被调用

[Click image to enlarge]

(点击图片放大)

#3


7  

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,那么它不会抛出错误,因为它是正确的语法,但是它不会工作,因为它是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
}

#4


5  

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

无论它是模态Segue还是Push Segue,代码下面都会调用它

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

#5


1  

I had a similar problem with UICollectionViewCell as 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 Class for the CollectionViewCell on the story board.

UICollectionViewCell作为segue的源,我也遇到了类似的问题。似乎在故事板segue到工作(没有performSegueWithIdentifier)时,需要有一个UICollectionViewCell的自定义子类,并将它用作故事板上的CollectionViewCell的类。

#6


1  

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的视图控制器的故事板中的类名下设置模块。它被设置为none,一旦我单击模块字段,它就被设置为正确的项目/模块并解决问题。

#7


1  

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. 确保您有正确的方法签名。它在Swift 3中发生了变化。
  3. 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.
  4. 然后确保连接按钮(或触发segue的任何东西)的方式与在故事板中连接segue的方式相匹配。有时你调用一个按钮,但是没有正确地将segue从这个按钮连接到目标视图控制器。
  5. 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.
  6. 确保segue的标识符是正确的。虽然这不是prepareForSegue没有被调用的原因,但这只是一个特定的segue没有被调用的原因。

#8


1  

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表单视图控制器= 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 before:

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

Voila!

瞧!

#9


0  

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()。

#10


-1  

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。

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;
    }
}

#1


26  

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中放置断点,即使这样也没有中断,因此我怀疑在故事板中,我没有与实现按钮的视图相关联的正确类。

#2


9  

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 prepareForSegue might not be called. Ensure you've:

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

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

    连接表视图原型单元格中的segue,而不是表视图控制器。

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

    在segue连接弹出菜单的“选择segue”组下使用segue,而不是在“辅助动作”下。

当我不使用performSegueWithIdentifier点击一个按钮时,prepareForSegue不会被调用

[Click image to enlarge]

(点击图片放大)

#3


7  

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,那么它不会抛出错误,因为它是正确的语法,但是它不会工作,因为它是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
}

#4


5  

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

无论它是模态Segue还是Push Segue,代码下面都会调用它

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

#5


1  

I had a similar problem with UICollectionViewCell as 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 Class for the CollectionViewCell on the story board.

UICollectionViewCell作为segue的源,我也遇到了类似的问题。似乎在故事板segue到工作(没有performSegueWithIdentifier)时,需要有一个UICollectionViewCell的自定义子类,并将它用作故事板上的CollectionViewCell的类。

#6


1  

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的视图控制器的故事板中的类名下设置模块。它被设置为none,一旦我单击模块字段,它就被设置为正确的项目/模块并解决问题。

#7


1  

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. 确保您有正确的方法签名。它在Swift 3中发生了变化。
  3. 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.
  4. 然后确保连接按钮(或触发segue的任何东西)的方式与在故事板中连接segue的方式相匹配。有时你调用一个按钮,但是没有正确地将segue从这个按钮连接到目标视图控制器。
  5. 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.
  6. 确保segue的标识符是正确的。虽然这不是prepareForSegue没有被调用的原因,但这只是一个特定的segue没有被调用的原因。

#8


1  

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表单视图控制器= 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 before:

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

Voila!

瞧!

#9


0  

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()。

#10


-1  

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。

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;
    }
}