2
votes

I have a question regarding storyboard segues. Platform is iOS 6.1. Target device is iPhone. IDE is Xcode 4.5.2. What I have are 2 TableViewControllers, both of which have a segue (segOne & segTwo) to the same ViewController. The desired outcome is that when a user navigates to this ViewController, they will return to the originating TableViewController. To do this I need some way to pass the id of the originating segue (or TableViewController) to the destination ViewController. In the past I accomplished this by setting up the 'back' button in the NavigationItem which always worked fine. Now, however I want to code the back buttons as separate controls.

I understand that to accomplish this I need to set up the 'prepareForSegue' method in the source TableViewControllers which I have done. Where I am having a problem is writing the code in the destinationViewControllers to programmatically identify which of the two segues is the source segue, segOne or segTwo? Both segues are identified as 'segOne' and 'segTwo' in Storyboard Seque. Both are style 'push' segues and both are destination 'current' segues. Each of the TableViewController implementation files are identical except for the segue ids. Here's my code:

header file:

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

implementation file:

-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
  if([[segue identifier] isEqualToString:@"segOne"])
  {
      NSLog (@"TableViewController1: segue.identifier %@", segue.identifier);
      NSLog (@"TableViewController1: self %@", self);
      NSLog (@"TableViewController1: sender %@", (id)sender);
  }
}

The output is exactly what I would expect. Here it is forTableViewController1:

TableViewController1: segue.identifier segOne
TableViewController1: self <TableViewController1: 0x928eb70>
TableViewController1: sender <UITableViewCell: 0x927a3e0; frame = (0 22; 768 44); text = '  1. birth month and year...'; autoresize = W; layer = <CALayer: 0x928d380>>

So, what I need is the code for the ViewController that identifies which of the two segues, segOne or segTwo is the originating segue. I do understand that any code needs to go in the viewDidAppear method of the destination ViewController. Once I have this it will be a simple matter to code the IBAction in the ViewController to navigate to the originating TableViewController. Any code examples on how to do this would be appreciated. thx...

1
Are you using a navigationViewController? If you do, just pop the view controller when you want to go back.Hjalmar
I would use also an UINavigationViewControllerGaroal
Yes I am using a UINavigationController.Customsoft

1 Answers

3
votes

When you push a view controller using a Navigation view controller, it will be pushed on the top of a stack with view controllers.

So all you have to do get back to the previous view controller is to pop the one that got pushed.

Put this in your IBAction method:

[self.navigationController popViewControllerAnimated:YES];

Now, if you really want to know what segue that was used, you can just pass the segue identifier to the destination controller, and put it in a property like this:


-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"segOne"])
    {
        MyOtherViewController *other = (MyOtherViewController *)segue.destinationViewController;
        other.theSegueIUsed = @"segOne";
    }
}

You also need to add the property to the other view controllers header file:

@property (strong, nonatomic)NSString *theSegueIUsed;