Unwind segues not working with modal view controllers - ios

I'm working on a storyboard-based iPhone app.
I am successfully using unwind segues to navigate my stack of view controllers.
However, when I present a modal view controller I cannot seem to be able to trigger the unwind segue that I have specified in the storyboard file.
Is this normal or a bug?

Problem solved.
Apparently I've hit some glitch with the storyboard editor.
What I did was to delete the destination (modal) view controller and then re-create it.
Works fine, now.
Bug report filed.

So I have the same problem (more than two years later!). I haven't fixed the underlying issue, but I found that a modal view being presented with a default presentation style will unwind OK, but one that uses a page sheet presentation style simply doesn't work. The unwindToViewX method gets called on the parent view controller that the unwind segue is moving to, though, so I've circumvented the issue with the following code:
if (self.presentedViewController) {
[self dismissViewControllerAnimated:YES completion:NULL];
}
This saves me from setting up a delegate system as the unwind action is doing all the heavy lifting, but also avoids an issue where perhaps the unwind action might work one day, because in that case self.presentedViewController should return NO because the unwind action worked correctly and we won't end up dismissing two view controllers by accident.
I hope this helps others in the same boat, but I'd also like to hear if anyone else has better solutions.

Related

Programmatically calling performSegueWithIdentifier does nothing

I am in a situation exactly like this post, but for whatever reason, only one of my performSegueWithIdentifier:sender works.
I have a View Controller (embedded in a UINavigationController) with two buttons, Cancel and Done. When clicked I want Cancel to unwind to one screen, and Done to unwind to another screen; however, this only works with one of the two segues I have set up.
- (IBAction)clickedDone:(id)sender {
[self performSegueWithIdentifier:#"unwindToHome" sender:self];
}
- (IBAction)clickedCancel:(id)sender {
[self performSegueWithIdentifier:#"unwindToViewEntries" sender:self];
}
"unwindToHome" works fine while "unwindToViewEntries" does nothing. No error, no printing, nothing. If I switch the segues and buttons, "unwindToHome" will still work while "unwindToViewEntries" does nothing.
I have unwind methods in both ViewControllers I want to unwind to and there are no other actions attached to the buttons.
I find it really odd that only one works. I've looked them over several times and there is nothing different that I can see.
Thanks in advance for the help. If there are some more things I should check please let me know!
I've already looked at solutions for the following posts:
How to unwind programmatically to 2 different VC
performSegueWithIdentifier not working
I've also tried removing the segues and re-adding them, as well as deleting and re-adding the entire view controller. Every time, the same behavior.
EDIT: I should also mention that I've tried adding other segues in different views and none of the new ones I add work. Only "unwindToHome" seems to work, and I can't figure out why.
In the storyboard make sure that both segues show that it is a show (push) segues. There was a question earlier,which is a little bit different, that if you create two segues from the same button Xcode was setting one push the other modal.
Happy coding!
Yan

Splitview segue and unwinding

I'm using a weird setup in my splitview Ipad app where i need some help with.
I have a TVC and a Normal View side by side in the split view. When i select something in my TVC both the screens segue to another TVC and Normal View. This part works fine. I'm using the push method with destination Master Split & Detail Split.
The problem is going back. I want to press 1 button and unwind both segues. I do this by this method: https://github.com/bradley/iOSUnwindSegueProgramatically[1]
This has 1 problem tho. The button being clicked to activate my segues is located in the detail view. So the detail view segues back no problem. The Master view however does not segue i'm stuck on how to get that working the same way.
The other option i figured i could do is do a normal segue and than use a custom transition animation but that seems like more of a "hack". So i was wondering if i could segue my TVC back through the detail view.
I hope i'm being clear on how i'm setup and where i need to go.
Cheers,
There is a note in Matt Neuburg's iOS7 book that in some situations you need to subclass UISplitViewController, and put the unwind method in the subclass. You might try that (but be sure to remove it from the detail controller, since only the first one in the chain is called.)

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).

iOS Showing views conditionally

I have a view in which a user selects an action to take and on that next screen there is a save and a back button. For both of the buttons the last line is dismissViewControllerAnimated:.
I need a way to make the 1st screen show only if the back button is used. save should send back to the main screen/rootViewController I am fairly new to iOS but not programming in general and just need a nudge in the right direction.
Could I set a bool flag to show or not? Maybe I can set the Tag on the view and then check that in the other screens on save/back? I assume I can check the parent view.
Sorry if this is a dup but I cant find anything specifically for this.
EDIT: I am not using a nav controller and am showing the views modally.
The answer will vary depending on how your UIViewControllers are structured and setup. If you're using a uinavigationcontroller then you can POP to the root view controller using:
[self.navigationController popViewControllerAnimated:YES];
If you're presenting your UIViewControllers modally, you can try to dismiss the presenting View Controllers of your modal view controller using the presentingViewController property:
[[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];
You may also want to take a look at Unwind Segues if you're using a Storyboard:
What are Unwind segues for and how do you use them?
Finally, as far as determining whether the back button is pressed or another button - that depends on how the app is setup. You'll need to use your own logic (probably if / then statements or case / switch) to determine which button was pressed. You also may want to check out the sender argument in IBActions.
John, to have a UINavigationViewController return to it's root viewcontroller, you use:
[nameOfNavController popToRootViewControllerAnimated:YES];
The other guys are correct that the information you've provided is definitely not enough to determine exactly what you need to do.
You can use the presentingViewController property of a modal view controller to access it's presenting controller.
It turns out that I was using the terminology wrong. I am presenting all views modally and that is the issue, there is no navigation controller. I ended up using NSNotification to build a listener and had the main view controller listen and then dismiss the view and hence show itself. Worked a treat.
here is the link to the code I ended up with.
http://iphonedevsdk.com/discussion/114737/view-heirarchy-issues-possibly-from-the-camera
Hopefully this helps someone else.

Handling segues/storyboards with memory intensive app

I have an app that has the following basic layout, please understand I have done a lot of programming, but I am relatively new to IOS and am yet to wrap my head around the Storyboards/segues properly yet.
Effectively, my app has the following screens:
WelcomeViewConroller ---ModalSeque--> MenuViewController --modalSegue---> newProjectVC || loadprojectVC ---modalSegue-->ProjectScreenVC.
From the project the screen, the user can return to the menuVC screen.
Now, I understand that every segue creates a new instance of a view controller, which is great, I want this to happen, however, when I segue back from my ProjectScreen, and then reenter it again, I get a huge memory leak and very strange behaviour.
I understand that I need to dismiss my View controllers, especially my ProjectScreen when I leave it, however, I can not get this to happen, no matter what I try.
Any help would be greatly appreciated.
In How should I chain viewcontrollers in xcode storyboard? I enumerate a series of ways of going back multiple scenes in a storyboard. But in short, the two easiest options are:
Unwind segues: If only supporting iOS 6 or higher, use unwind segues. So, for example, in your main menu's view controller, implement a unwind segue:
- (IBAction)gotoMainMenu:(UIStoryboardSegue *)segue
{
// if you need to do anything when you return to the main menu, do it here
}
Also make sure to define that in the main menu's .h. Then control+drag from the button that you want the segue to the "exit" button in the panel below the scene, and choose the "gotoMainMenu" option:
Navigation controller: If you need iOS 5 support, then just use a navigation controller and replace the modal segues with push segues. Then you can use popToViewController or popToRootViewControllerAnimated to jump back multiple scenes. If you don't want to show the navigation bar, then select the navigation controller in your storyboard, and in the attributes inspector, uncheck "Shows Navigation Bar":
In this scenario, I actually think it's easiest to make sure your menu scene is the root (and have it do a little detour to the welcome screen, like I discuss in point 4 of that other answer), in which case you can just call popToRootViewController whenever you want to return to the main menu. But, if the main menu is not the root view controller, and you want to pop back to it, you can either pass a point menu controller from scene to scene, or you can have subsequently presented view controllers do something like the following when they want to get back to the main menu:
for (UIViewController *controller in [self.navigationController viewControllers])
{
if ([controller isKindOfClass:[MenuViewController class]])
{
[self.navigationController popToViewController:controller animated:YES];
break;
}
}
Elsewhere on Stack Overflow, you'll see people contemplating ways to nest calls to dismissViewControllerAnimated, or other variations like that. I personally think that navigation controllers and unwind segues are far easier and more elegant.

Resources