performSegueWithIdentifier doesn't work
Asked Answered
S

2

8

My main view controller is in a navigation controller and it conforms to EditViewControllerDelegate protocol. It is the delegate of my two view controllers that I need to present modally.

@interface EditViewController : UIViewController
@property (nonatomic) id <EditViewControllerDelegate> delegate;
@end

@interface EditSomethingViewController : EditViewController
@end

@interface EditSomethingElseViewController : EditViewController
@end

In a editViewController:(EditViewController *)evc didFinishEditing:(Something *) something method, I first get the data I need then I dismiss the evc and call

[self performSegueWithIdentifier:@"My Segue" sender:self];

"My Segue" is defined in Xcode and the identifier is the same both in code and in Xcode (I tried to change it just to see if it gets called and it throws an exception)

When I change "My Seque"'s type to push, it worked. But with modal it doesn't do anything after I'm back to the main view controller

What am I missing?

EDITED:

I accidentally found out a warning in my storyboard! (it's weird because it's not a warning in the project "visible from everywhere") In the connections' inspector under "Referencing Storyboard Segues" there's a warning for my modal segue. it says :

(null) is not a valid containment controller key path

I checked other modal segues and there is the same warning, but I didn't need to trigger them by code so didn't have problems before.

EDITED 2:

-(void)editViewController:(EditViewController *) evc
didFinishEditing:(Something *) something
{
    self.something = something;
    [self dismissModalViewControllerAnimated:YES];
    For ( OtherThing * otherThing in self.something.otherthingsArray)
    {
        NSLog(@"%@", otherThing);
    }
    [self performSegueWithIdentifier:@"My Segue" sender:self];
}
Sismondi answered 19/4, 2012 at 17:25 Comment(6)
Can you show us your whole editViewController:(EditViewController *)evc didFinishEditing:(Something *)something method?Accost
For now it does nothing else apart from an NSLog to see "something"'s description. I just added an NSlog to prepareForSegue and it gets called but the segue is not performed on screen.Sismondi
I thought you said you were dismissing the edit view controller and calling performSegueWithIdentifier in that method. Where are you doing those things? I want to see how you do that part to see if thats where the mistake is.Accost
Yes sure! but I meant nothing more. I'm gonna add it to the question just to remove ambiguity.(EDIT 2)Sismondi
how should the view perform: [self performSegueWithIdentifier:@"My Segue" sender:self]; if it's dismissing himself before [self dismissModalViewControllerAnimated:YES]; ?Aymer
@Schnarchii : it's the presenting ViewController that dismisses the modal one.Sismondi
A
21

You need to wait until your other view controller is done animating, before performing the segue. You can use the new iOS 5 method:

[self dismissViewControllerAnimated:YES completion:^() {
    [self performSegueWithIdentifier:@"My Segue" sender:self];
}];

If you need a pre-iOS 5 way to do it, you should just add a delay to give the animation time before performing the segue.

Accost answered 21/4, 2012 at 18:45 Comment(7)
Silly me... if you are using performSegueWithIdentifier:, you must be using iOS5, so you should definitely be using dissmissViewControllerAnimated:completion: too!Accost
Also, I don't know if this will solve all your problems, but its a step in the right direction - you definitely should be doing it this way.Accost
Perfect! now it works.Thank you!. I guess it's about animations being run in a different thread, right? So the performSegueWithIdentifier was called but before the animations completes... I tried to run it with dismissModalViewControllerAnimated:NO and it worked as well.Sismondi
any clue about the "(null) is not a valid containment controller key path" ?Sismondi
I think that "(null)" warning is just an Xcode bug. Nothing to worry about.Accost
Yeah, dismissModalViewControllerAnimated:NO should work too, but dismissModalViewControllerAnimated: is actually deprecated, so even if you don't want to animate, you should still use the code I gave you, just with NO instead of YES.Accost
Thank you for the info! I didn't know it was deprecated. in the UIViewController class reference it's marked as deprecated only in the detail part and not the "tasks" section.Sismondi
W
0

In my case, I was able to perform the segue once successfully; but not subsequent times. There was no particular fatal error thrown, and use of the DispatchQueue.main.async did not help me either.

Eventually, I remembered I had it working on a separate branch at one point in time. The resolution ended up being pretty silly; where I was using a "Show Detail" style of segue, I had to change it to a "Present Modally" segue. This, for whatever reason, allowed me to perform it multiple times again.

W answered 30/10, 2019 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.