سؤال

in my current application, I want to use a certain UICollectionView several times, but with different selection behaviours. Consider the following storyboard layout as "is" (and working):

Tab Bar Controller (2 items):
-> Navigation Controller 1 -> Collection View Controller -> some Table View Controller
-> Navigation Controller 2 -> (Basic) View Controller

The Basic View Controller has two UIButtons which have Storyboard Push-connections to the Collection View Controller. What I want is to transition from the Basic View Controller to the Collection View Controller, but selecting an item from the collection should pop the view and return to the Basic View Controller.

I have set a custom property in the Collection View Controller which gets set in the corresponding prepareForSegue message of the Basic View Controller (or not at all, if the user selects a Tab Bar Item), so there's no problem in detecting which controller or which UI component triggered the push (there are 3 ways: selecting the tab bar item or tapping one of the buttons on Basic View).

The problem is popping the Collection View.

My code so far:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ( self.mode == nil ) {
        // do nothing
    } else if ( [self.mode isEqualToString:@"foobar"] ) {
        // one way I tried
        [self dismissViewControllerAnimated:YES completion:nil];
    } else if ( [self.mode isEqualToString:@"blah"] ) {
        // other method
        BasicViewController *targetVC = self.navigationController.viewControllers[ 0 ];
        [self.navigationController popToViewController:targetVC animated:YES];
    }
}

Unfortunately, my app crashes in the lines dismiss resp. popToViewController. Is it even possible to use the same view controllers in different ways of navigation?

I hope it's enough information to help me out on this one. As you might know, projects grow, and I don't know if there's more code to consider :)

Thanks in advance!

هل كانت مفيدة؟

المحلول

The prepareForSegue: method is not the right place to put that code. It's called right before the segue is performed, which usually means there's allready some kind of transition about to happen.

I'm assuming you've connected your collection view cells with a segue and are now trying to modify the behaviour of that segue depending on the viewcontroller 'before' the collectionVC.

In that case there are some possible solutions:

  • don't use segues for that specific transition and do any vc-transitions manually
  • write your own UIStoryboardSegue subclass that can handle the different transitions. See UIStoryboardSegue Class Reference. Then you can set some property of your custom segue in the prepareForSegue: method, to tell the segue which transition it should perform.
  • use unwinding segues. See What are Unwind segues for and how to use them?

نصائح أخرى

The action of a selected a row in your 'Collection View Controller' differs depending on from where the 'Collection View Controller' was presented. In one case, return to 'Basic View Controller' and in the other segue to 'some Table View Controller'

Implement this by defining tableView:didSelectRowAtIndexPath: with something like:

- (void)        tableView: (UITableView *) tableView 
  didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
   if (self.parentIsBasicViewController)  // set in the incoming segue, probably.
      [self.presentingviewController dismissViewControllerAnimacted: YES completion: nil]
   else
      [self performSegueWithIdentifier: @"some Table View Controller" sender: self];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top