shouldperformSegueWithIdentifier 不能正常工作

Posted

技术标签:

【中文标题】shouldperformSegueWithIdentifier 不能正常工作【英文标题】:shouldperformSegueWithIdentifier does not work properly 【发布时间】:2017-06-06 13:58:39 【问题描述】:

我试图了解“应该如何执行SegueWithIdentifier”。 在storyBoard中,我创建了三个场景“scene1,scene2,scene3”。 Scene1 通过名为“scene1To2”的 segue 链接到 scene2,scene2 通过名为“scene2To3”的 segue 链接到 scene3,scene3 通过名为“scene3To1”的 segue 链接到 scene1。

如下代码所示,我调用了“shouldPerformSegueWithIdentifier”,如果segue的标识符是“scene3To1”,它返回NO。

在运行时,我预计当我尝试通过“scene3To1”从scene3过渡到scene1时,过渡不会发生,但它是正常发生的……那么“shouldperformSegueWithIdentifier”的目的是什么

代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad 
 [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a      
 nib.
 

 - (void)didReceiveMemoryWarning 
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
 

 - (IBAction)from1To2:(id)sender 
    NSLog(@"in 1st scene");

       [self performSegueWithIdentifier:@"scene1To2" sender:self];
  

   - (IBAction)from2To3:(id)sender 
    NSLog(@"in 2nd scene");

    [self performSegueWithIdentifier:@"scene2To3" sender:self];
    

   - (IBAction)from3To1:(id)sender 
   [self performSegueWithIdentifier:@"scene3To1" sender:self];


   NSLog(@"in 3rd scene");
  


  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  
    NSLog(@"prepareForSegue");

     if ([segue.identifier isEqualToString:@"scene1To2"])
      
      NSLog(@"transiting from scene1To2");

      else if ([segue.identifier isEqualToString:@"scene2To3"])
     
     NSLog(@"transiting from scene2To3");

      else if ([segue.identifier isEqualToString:@"scene3To1"])

    
    NSLog(@"transition from scene 3 back to scene 1");
    

    

  -(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier  
   sender:(id)sender
   
   NSLog(@"shouldPerformSegueWithIdentifier");

   if ([identifier isEqualToString:@"scene3To1"])
   
    return NO;
    


  return YES;
   

  @end

输出

2017-06-06 16:02:07.301 UnwindSegue-1[699:19450] in 1st scene
2017-06-06 16:02:07.315 UnwindSegue-1[699:19450] prepareForSegue
2017-06-06 16:02:07.316 UnwindSegue-1[699:19450] transiting from  
scene1To2
2017-06-06 16:02:07.320 UnwindSegue-1[699:19450]    
shouldPerformSegueWithIdentifier
2017-06-06 16:02:08.245 UnwindSegue-1[699:19450] in 2nd scene

2017-06-06 16:02:08.260 UnwindSegue-1[699:19450] prepareForSegue
2017-06-06 16:02:08.260 UnwindSegue-1[699:19450] transiting from    
scene2To3

2017-06-06 16:02:08.265 UnwindSegue-1[699:19450]    
shouldPerformSegueWithIdentifier
2017-06-06 16:02:09.165 UnwindSegue-1[699:19450] prepareForSegue
2017-06-06 16:02:09.165 UnwindSegue-1[699:19450] transition from 
scene 3 back to scene 1
2017-06-06 16:02:09.167 UnwindSegue-1[699:19450] in 3rd scene
2017-06-06 16:02:09.167 UnwindSegue-1[699:19450] 
shouldPerformSegueWithIdentifier

*故事板**:

【问题讨论】:

这段代码是否在所有三个视图控制器中都重复了? shouldPerformSegueWithIdentifier: 甚至被调用了吗? from3To1: 被调用了吗? @Larme 请看上面的输出部分,我发布了我收到的输出 @DonMag 不,我有一个名为 ViewController 的类,三个视图控制器与之关联 嗯...我找不到这方面的文档,但似乎 shouldPerformSegueWithIdentifier 仅在自动触发 segue 时才被调用。 【参考方案1】:

问题是您的 Segue 已连接到一个按钮,但您有一个 IBAction 连接到该按钮...并且在您的 IBAction 代码中为同一个 Segue 手动调用 performSegueWithIdentifier

要创建一个您想要手动调用的 Segue,而不是从按钮上按住 ctrl 拖动,而是从控制器图标上按住 ctrl 拖动:

现在,您可以将该逻辑放入您的 IBAction 方法中,而不是使用 shouldPerformSegueWithIdentifier 来确定是否继续。

【讨论】:

以上是关于shouldperformSegueWithIdentifier 不能正常工作的主要内容,如果未能解决你的问题,请参考以下文章