Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Steven Rayzman
2,423 PointsHow to prepare for back button segue?
I am using a UINavigation controller, and when I press on the back button item, I need to execute a particular piece of code (To make the array empty again). How would I accomplish this using swift. Stone Preston
1 Answer
Stone Preston
42,016 Pointsyou should implement didMoveToParentViewCOntroller:
- (void)didMoveToParentViewController:(UIViewController *)parent
{
if (![parent isEqual:self.parentViewController]) {
NSLog(@"Back pressed");
}
}
in swift that would probably look something like:
override func didMoveToParentViewController(parent: UIViewController) {
if !parent.isEqual(self.parentVIewController) {
println("Back button pressed")
}
}
you may have to play around with that a bit.
Steven Rayzman
2,423 PointsSteven Rayzman
2,423 PointsHow would the code look, using swift? Thanks again
Stone Preston
42,016 PointsStone Preston
42,016 Pointssee my answer above. you are probably going to have to tweak that a bit.
Steven Rayzman
2,423 PointsSteven Rayzman
2,423 PointsHow could I make it so that when the back button of the UINavigation Controller is pressed, than the unwind segue is executed. Stone Preston
Stone Preston
42,016 PointsStone Preston
42,016 Pointsim not sure thats possible without creating your own UIBarButtonItem that acts as the back button. see this stack overflow post on how to create unwind segues. its very detailed with lots of screenshots.
Steven Rayzman
2,423 PointsSteven Rayzman
2,423 PointsThanks for the stack overflow post. Another way would be to just set the array to be empty once the back button is pressed. How could I do that? Stone Preston