0

I can't figure out why my segue.identifier is always returning null. I set the segue identifiers in the storboard.

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

The log message is always null so if statement is never true. Any thoughts why i cant get the identifier?

EDIT:

I made a whole new project for testing. It's a single view application with ViewController and SecondViewController. In the storyboard I made a popover segue connection from a button in View Controller to SecondViewController and set the segue identifier to segue1.

On SecondViewController I made a back button and connected it to the unwindFromSegue in my ViewController. I also added prepareForSegue in the SecondViewController.

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

Hope this helps. Thanks

Gulfer
  • 101
  • 1
  • 2
  • 11
  • IF you set the identifier on the correct segue, and the identifier is segue1, then this will work. So, one of those things is not right, or you set up the unwind segue incorrectly. Can you describe what you did? – rdelmar Apr 23 '13 at 04:58
  • I did have segue1 as the identifier for the popover segue. I added more to my question. Can you see any issues. Thanks – Gulfer Apr 23 '13 at 12:28
  • Checkout [Dismiss Popover Using Unwind Segue](http://stackoverflow.com/questions/13457296/dismiss-popover-using-unwind-segue-in-xcode-storyboard). – Michael Reneer Apr 23 '13 at 18:14

2 Answers2

2

When you set the segue in the storyboard you also have to assign it an identifier. In your case "segue1".

Michael Reneer
  • 2,271
  • 1
  • 16
  • 22
  • I did have segue1 as the identifier for the popover segue. I added more to my question. Can you see any issues? Thanks – Gulfer Apr 23 '13 at 12:28
  • Yeah, in your first question, you didn't include that you were talking about a popover segue. See this [question](http://stackoverflow.com/questions/13457296/dismiss-popover-using-unwind-segue-in-xcode-storyboard). – Michael Reneer Apr 23 '13 at 18:13
0

Why do you have an unwindFromSegue: method in SecondViewController? You're not unwinding to that controller, and it has the same name as the one in ViewController. That could be your problem. Take that one out and see if that fixes the problem.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Oops, I was testing moving the methods around and left that in there. On my latest trials I added prepareForSegue: to ViewController and that would show the segue.identifier as segue1 in ViewController. Should I be able to get segue.identifier in SecondViewController or would I need to pass it in from ViewController? – Gulfer Apr 23 '13 at 17:00
  • @Gulfer, you would have to pass it, but why would you? Why does SecondViewController need to know which segue it came from? – rdelmar Apr 23 '13 at 17:53
  • You right. I was wanting to get the segue identifier from SecondViewController and now I simply get it from ViewController. I would just be passing the same number back and forth. Thanks for the help. – Gulfer Apr 23 '13 at 21:24
  • Since you led me in the right path I will mark you answer as correct. I removed unwindFromSegue: from SecondViewController and added prepareForSegue: to ViewController where I can get the segue.identifier – Gulfer Apr 23 '13 at 21:26