Search code examples
iosuiviewcontrolleruinavigationcontrollersegueuistoryboardsegue

iOS - Segues and Static Cells


I have a group of Static Cells inside a TableView, And I have 3 of these cells (A, B, C) all set up with segues which push the Same ViewController onto the screen; This works fine!

What i want is a way for the View controller to identify which cell was clicked: through which the ViewController could alter what it presents. Specifically, I'd like an int (for say) in my viewController to be set to a particular value according to which cell was clicked

I'm guessing I'm missing something in the - (void) prepareForSegue method ?

Any help is very appreciated

The following Code is in My View Controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
 if ([segue.identifier isEqualToString:@"segue1"]){
    selectedSegue = 1;
 } else {
    selectedSegue = 2;
 }

  NSLog(@"Selected Segue: %d", selectedSegue); 
}

selectedSegue always returns 0;

I've set the Segue identifier in my storyboard as well


Solution

  • I think you are almost there. Check the following example implementation of prepareForSegue:sender: (this method signature might even be the problem!)

    #pragma mark - Storyboard Methods
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
        if([segue.identifier isEqualToString:@"segueOne"]) {
    
            BBFirstViewController *vcOne = (BBFirstViewController *)segue.destinationViewController;
            vcOne.someProperty = @1;
        }
        else if([segue.identifier isEqualToString:@"segueOne"]) {
    
            BBSecondViewController *vcTwo = (BBSecondViewController *)segue.destinationViewController;
           vcTwo.someProperty = @2;
       }
    }
    

    Then just make sure in the storyboard file each segue has its identifier set:

    Xcode Storyboard

    Click on the segue line in between the two view controllers and sets the identifier in the Attributes Inspector.