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 http://stackoverflow.com/a/1816682 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");
}
}

iPhoneFTP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s