I'm trying to check if the back button on my view controller was pressed but I'm having a hard time detecting this in Swift.
With this code:
if (contains(self.navigationController?.viewControllers, self)) {
println("Back button not pressed")
} else {
self.updateSearchQueryModel()
}
I am getting the error:
Could not find an overload for contains that accepts the supplied arguments.
I did get the result that I wanted in another fashion but I am still confused as to why this error is happening.
Why is this happening? Can I not check if self exists in an array?
Source of original code in Objective C that I couldn't translate to Swift: Setting action for back button in navigation controller
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
}
Please do not tell me how to detect that the back button was pressed. I already figured that out here.
Source of solution to objective: Detecting when the 'back' button is pressed on a navbar