我正在努力了解"应该如何执行SegueWithIdentifier"。 在storyBoard中,我创建了三个场景" scene1,scene2,scene3"。 Scene1通过名为" scene1To2"的segue链接到scene2,scene2通过名为" scene2To3"的segue链接到scene3。和scene3通过名为" scene3To1"的segue链接到scene1。
如下面的代码所示,我打电话给#34; shouldPerformSegueWithIdentifier"如果segue的标识符为" scene3To1"则返回NO。
在运行时,我预计当我尝试通过" scene3To1"从scene3转换到scene1时,转换不会发生,但它通常会发生...所以" shouldperformSegueWithIdentifier&的目的是什么? #34;
码:
#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
*故事板**: