How to overload the back button of a UITableViewController

Finally a technical post!

I was doing an iPhone assignment the other day where I am creating a simple FTP directory listing app. The problem was that I could change views during an FTP call (asynchronous) by hitting the back button, but the request would continue and then break my app with an “EXC_BAD_ACCESS” error. So I wanted to be able to run code on that back button press to stop FTP request.

However, this is not a simple thing to find out how to do! Some people on StackOverflow suggested putting in a custom back button with a selector pointing to a function, but the new button didn’t look as nice as the default one and lost the naming features. But then I came across this which suggests to use viewWillDisappear. I kill the FTP connection in this function (if its currently running) and now everything is peachy!

- (void)viewWillDisappear:(BOOL)animated {
NSArray *viewControllers = self.navigationController.viewControllers;
if ([viewControllers indexOfObject:self] == NSNotFound) {
if (self.activityIndicator.hidden == NO){
[self stopReceiveWithStatus:@"Cancelled"];

NSLog(@"View controller was popped");