我不明白为什么我的 segue.identifier 总是返回 null。我在 Storboard 中设置了 segue 标识符。
视图控制器1
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"prepared %@",segue.identifier);
if([[segue identifier] isEqualToString:@"segue1"] ){
NSLog(@"segue is equal to 1");
}
}
视图控制器2
- (IBAction)unwindFromSegue:(UIStoryboardSegue *)segue {
NSLog(@"unwinded %@",segue.identifier);
}
日志消息始终为空,因此 if 语句永远不会为真。有什么想法为什么我无法获得标识符?
编辑:
我做了一个全新的测试项目。这是一个带有 ViewController 和 SecondViewController 的单视图应用程序。在情节提要中,我从 View Controller 中的一个按钮到 SecondViewController 建立了一个弹出框 segue 连接,并将 segue 标识符设置为 segue1。
在 SecondViewController 上,我制作了一个后退按钮并将其连接到我的 ViewController 中的 unwindFromSegue。我还在 SecondViewController 中添加了 prepareForSegue。
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
视图控制器.m
#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)unwindFromSegue:(UIStoryboardSegue *)segue {
NSLog(@"unwinded %@",segue.identifier);
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@end
第二视图控制器.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)
nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//was just testing unwindFromSegue here. Didn't mean to post here,
//but I will keep it in with an answer referring to it
//- (IBAction)unwindFromSegue:(UIStoryboardSegue *)segue{
//NSLog(@"unwind in second %@",segue.identifier);
//}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(@"prepare %@",segue.identifier);
}
@end
希望这可以帮助。谢谢