我无法弄清楚为什么我的segue.identifier总是返回null。我在storboard中设置了segue标识符。
ViewController1
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"prepared %@",segue.identifier);
if([[segue identifier] isEqualToString:@"segue1"] ){
NSLog(@"segue is equal to 1");
}
}
ViewController2
- (IBAction)unwindFromSegue:(UIStoryboardSegue *)segue {
NSLog(@"unwinded %@",segue.identifier);
}
日志消息始终为null,因此如果statement永远不为true。有什么想法我无法获得标识符?
编辑:
我制作了一个全新的测试项目。它是ViewController和SecondViewController的单一视图应用程序。在故事板中,我从View Controller中的按钮到SecondViewController进行了popover segue连接,并将segue标识符设置为segue1。
在SecondViewController上,我创建了一个后退按钮并将其连接到我的ViewController中的unwindFromSegue。我还在SecondViewController中添加了prepareForSegue。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.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
SecondViewController.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
希望这会有所帮助。感谢
答案 0 :(得分:2)
在故事板中设置segue时,您还必须为其分配标识符。在您的情况下“segue1”。
答案 1 :(得分:0)
为什么在SecondViewController中有unwindFromSegue:方法?您没有展开该控制器,它与ViewController中的控制器名称相同。那可能是你的问题。拿出那个,看看是否能解决问题。