Default button of navigation gives you functionality to move to the previous view
You can check it by :
override func didMoveToParentViewController(parent: UIViewController?) {
if parent == nil {
println("Back Pressed")
}
}
But the situation is didMoveToParentViewController
means it already move to the previous view.
You can add custom button as back button in your navigation bar. Then your issue will be solved.

Add button Programmatically :
let backButton = UIBarButtonItem(title: "Назад в будущее", style: .Plain, target: self, action: "toMainFromGallery")
self.navigationItem.leftBarButtonItem = backButton
Function :
func toMainFromGallery {
}
Button With Back Icon :
var backButton = UIButton(frame: CGRectMake(0, 0, 70.0, 70.0))
var backImage = UIImage(named: "backBtn")
backButton.setImage(backImage, forState: UIControlState.Normal)
backButton.titleEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)
backButton.setTitle("Back", forState: UIControlState.Normal)
backButton.addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
var backBarButton = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = backBarButton
There should be space between left If you want to remove it add spacer like as :
var spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
spacer.width = -15
self.navigationItem.leftBarButtonItems = [spacer,backBarButton]
Back Icon Image should be in 22px, 44px and 66px = @1x , @2x and 3x respectivaly