Wednesday, 20 May 2015

swift navbar back button clicked event

  override func viewWillDisappear(animated : Bool) {
      println("isMovingToParentViewController")  never get called
      println("isMovingFromParentViewController")    ---this is the key function
    if (isBeingDismissed()) {
      println("isBeingDismissed")     this function never get called
    if (isBeingPresented()) {
      println("isBeingPresented")  never get called
"isMovingFromParentViewController" is a very confusing function name.

It is a status flag, if you check it in viewWillAppear() function is already set.

So, it is a status flag to indicate it is a navigate page from navigated from root page。


  override func willMoveToParentViewController(parent: UIViewController?) {
    println("This VC has has been pushed OR popped")
    if parent == nil {
      println("This happens ONLY when it's popped")


No matter this page will show or disappear, this function will get called.

But parent will equal to nil, if it is going to disappear and back to previous page.

This function page is more confusing that the function above.

But if you think about the previous page is the parent page that will be fine.


  override func viewDidLoad() {
    self.navigationItem.setHidesBackButton(true, animated: false)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
  // Then handle the button selection
  func goBack() {
    // Probably want to show an activity indicator


By this method, you created your own back button. so you may have risks of loosing style control and some build-in features such as showing previous page's title.

No comments:

Post a comment