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

iOS

How 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

see this stack overflow post

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

How would the code look, using swift? Thanks again

see my answer above. you are probably going to have to tweak that a bit.

How could I make it so that when the back button of the UINavigation Controller is pressed, than the unwind segue is executed. Stone Preston

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

Thanks 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