Second Segue from an Unwind Segue Action - ios

My view controller (A) issues a perform modal segue to view controller (B). The subsequent unwind segue action issues a second perform modal segue to what I expect to be another instance of the same view controller (B). That is, I want (B) to do it's thing a second time independent of the first segue to (B). While prepareForSegue is called a second time, the second segue is never executed. View (B) hangs around until the unwind segue action and the methods it calls complete, and the second perform segue and prepareForSegue are ignored.
The only way I can think to avoid this problem is to issue a perform block after a delay allowing the return segue et. al. to go to completion.
Is there a better way?
Thanks

If I understand, you are calling the segue to B from view controller B like so?
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
// Code
[self performSegueWithIdentifier:#"segueToB"];
}
You can't go to view B because you are already there. You will need to wait until B has been fully dismissed and call it from view A.

I don't understand why this happens, but now it works. I have done nothing since yesterday other than add a few breakpoints. This is not the first time adding breakpoints seems to fix a problem.

Related

Whats the difference between moving between Storyboard scenes - Segues and Unwind

I'm a fresher. I used segue to go (Flow of Execution) such like that
UIViewController A => UIViewController B => UIViewController C.
And used unwind segue to move from C to A. But I am confused about how they different of each other.
And Why I use unwind segue although I have delegation.
Thank You in advance.
Regular Segue allows us to send data from one view controller to another, but it’s not easy to bring data back if the user has changed or added details in that view controller. That’s where an Unwind Segue comes in.
Read more here
Segues are for presenting a new view controller.
Unwind Segue is a special kind of segue (A go back mechanism) which moves back to the connected destination.
A->B->C->A
If you are using segue for above mentioned flow, there will be two different instance of A in the memory.
If you use unwind segue for the C->A transition
It act as a go back mechanism and it removes the C and B instance from the memory once the segue is over
There will be only 1 instance of A

Middle ViewControllers Loading During Unwind

In VC-A I have a IBAction called "unwindToMain". A second VC, VC-B, is loaded and then a third VC. In VC-C I call that unwind function from a button press.
The problem is that VC-B appears to be loaded during the unwind. I added some print() statements to help debug and I can see VC-B code, including the tableview functions, getting run after the button click, before I arrive at VC-A.
Why is this happening and how do I stop it? Thanks in advance!
This is how unwind segues work. You get from C to get to A by going through B. The only solution that I can think of is creating a prepareForSegue method going from C to A.

Why isn't my segue being performed?

I have a segue that should take place when one of a number of things happen, so it's called programatically, like so:
- (void)unwindAway
{
NSLog(#"Let's segue");
[self performSegueWithIdentifier:#"mySegue" sender:self];
NSLog(#"We should have just performed the segue");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"Let's do a segue");
}
but the output I get in the console is:
2014-05-29 22:20:30.173 My App[7848:60b] Let's segue
2014-05-29 22:20:30.178 My App[7848:60b] We should have just performed the segue
so as you can see, it's not even calling prepareForSegue.
The segue name is correct - if I give an invalid segue name it errors as you'd expect.
Any ideas?
For unwind segues, prepareForSegue:sender: is called on the view controller that was the source of the segue, in other words the one you're exiting from.
As per Rob's suggestion in the comments I checked the name of the method in the destination View Controller in the segue. It looked right (and was selected in IB rather than typed) but pasting over it and recompiling fixed the problem. Something must have been messed up in the source code of the storyboard, perhaps an artefact of renaming methods.
It's worth noting if anyone has a similar issue that the app won't generate any error if the destination method of a segue isn't found anywhere (I've confirmed this by typing a nonsense method name).
I know that your issue has been fixed, but for future reference I just want to say that similar problems might be caused by the way the unwind process works (zie the technical note link above).
As soon as the segue has been triggered in a certain view controller, its parent (!) view controller is called with the message: viewControllerForUnwindSegueAction:fromViewController:withSender:. The implementation checks if the parent wants to handle the unwind action. If not, it's array with child view controllers is searched for a view controller that wants to handle the action.
My problem was that the unwind action was implemented in a child view controller of a view controller that was embedded in a navigation controller. So, when te segue began, the navigation controller (the parent) was asked: will you handle the action? It returned NO. Then, it's children were asked the same. It returned NO. Because the message isn't sent to a child view controller of a child of the parent view controller, there isn't a view controller that will handle the unwind action and it is aborted without an error message.
My solution was to implement the unwind action in the view controller itself and not in it's child view controller.

My Navigation Controller Count Keeps Growing

I'm new to iOS dev and am not entirely sure on Storyboards/Segues/Pushing and Popping.
So in my app I have a navigation controller with 5 view controllers leading from one to another.
When it reaches the last view controller i have a segue to the first and I have a few tasks listed in the prepareForSegue method.
Out of curiosity I decided to check what happens to the [self.navigationController.viewControllers count]. I found that it keeps growing and growing which doesn't 'feel' correct.
Am i handling going back to the first screen correctly? The prepareForSegue method is useful as it allows me to send some data back to the first segue. Is it possible to maybe say when you go back clear all views on that navigation controller?
Thanks
You can use an unwind segue. Here's a good tutorial:
pragmaticstudio.com/blog/2013/2/5/unwind-segues
Make sure to create the unwind action method before you wire it up in the storyboard otherwise it won't show up when you drag to 'Exit'. That was the most confusing part for me when I first set one up. The tutorial does it in the correct order so if you follow it you should be fine.
Also, here's a sample I put together showing how to transfer data back in an unwind segue. It uses a modally presented view controller but the technique is the same:
github.com/soleares/AddToTableView
No, you should never go backwards with a segue (other than an unwind). Segues ALWAYS instantiate new controllers, so you're not actually going back to the first controller, you're just creating a new instance, which gets added to the stack. So either go back with an unwind segue or use popToViewController:animated:. Using an unwind segue will allow you to use prepareForSegue, and it does cause all the controllers in between to be deallocated (if you have no other strong pointers to them).

IOS7 - performSegueWIthIdentifier does not have any effect

I have a problem in iOS7 where I am calling a segue with performSegueWithIdentifier (I have code just like this that works just about everywhere else), then I log the segue in prepareForSegue, then I log again the view controller (VC) that the segue is supposed to push to the top.
prepareForSegue gets called appropriately and the segue has the correct string as its identifier property. Yet the VC that it is supposed to push to the top never gets initialized nor viewWillAppear gets called.
The segue I am talking about, which is the only one that does not work (all the other ones work in both ios6 and 7), is the one leading form the center VC to the right VC. By the way, this works flawlessly in iOS6.
What could be the cause?
the code:
-(IBAction)gotoMainMenu:(id)sender{
DLog(#"DifferentName");
[self performSegueWithIdentifier:#"DifferentName" sender:self];
}
Get in the habit of not wiring up segue's to buttons. Wire them up to the VC and then in the touchUpInside action, fire off the performSegueWithIdentifier.
I had the same issue and solved it as follows. In view A I had a segue that was triggered by a button (UIButton) and the button was also connected to an action in my controller. When I clicked the button in View A, View B would appear as expected. However, when I tried clicking a button in View B to go to View C nothing happened just as you described above.
In my case the issue was resolved in View A. I removed the segue that was tied to the button and let the IBAction that was associated with the button handle calling the performSegueWithIdentifier, then I created a new manual segue that was only tied to the view and voila things worked as expected again.
In short, I guess make sure you don't have both and action and a segue linked to the same button. Hope this helps.

Resources