Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

ios - Unwind segues for 2 View Controllers

I have one button (back button) on a view controller. Simple so far. I have 2 view controllers with a table on each one. if a user selects a row from either table it goes to the view controller with the back button on. The back button needs to go back to the original view controller that the user was on when they selected the row.

I was considering unwind segues for this which is fine, but I cannot add two segues to one button. One for return to one table view and a return for the other table view dependent on which table view they used to access the view controller.

Any ideas ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Volk explained,

-(IBAction)devDismiss
    {
    NSLog(@" ------- dev dismiss .....");

    // for a custom segue unwind, you must do this...
    [self performSegueWithIdentifier:@"specialWord" sender:self];

    // "specialWord" is the identifier set on storyboard for the
    // custom unwind segue

    /* for a "default" unwind segue, do this...
    [self dismissViewControllerAnimated:YES completion:nil];
    Note, if you used a push segue, you should use
      [self.navigationController popViewControllerAnimated:YES]
    If you used a modal segue, you should use
      [self dismissViewControllerAnimated:YES completion:nil] "
    */
    }

Note that indeed, you must also use "specialWord" in your segueForUnwindingToViewController: override, which will be in the DESTINATION (that is to say the ORIGINAL) view controller, the one underneath, that is to say.

-(UIStoryboardSegue *)segueForUnwindingToViewController:
                          (UIViewController *)toViewController 
              fromViewController:(UIViewController *)fromViewController 
              identifier:(NSString *)identifier
    {
    NSLog(@"we're in _your class name_, segueForUnwindingToViewController %@",
      identifier);

    // for some unwinds, we have a custom unwind we want to use.
    // so, check the identifier:
    if ([identifier isEqualToString:@"specialWord"])
        {
        YourCustomUnwindSegue *sg = [[YourCustomUnwindSegue alloc] 
                           initWithIdentifier:identifier 
                           source:fromViewController 
                           destination:toViewController];
        return sg;
        }

    // don't forget the break-away "return" inside any macthes.

    // NSLog(@"note, if this message appears, it's likely you have a typo
    //  somewhere for 'specialWord' - unless you genuinely have a situation
    //  where it will also fall through to the 'default' unwind segue :O ");

    // BE SURE TO return the default unwind segue otherwise
    return [super segueForUnwindingToViewController:toViewController 
                                 fromViewController:fromViewController
                                         identifier:identifier];
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...