1

I have created a new class to implement UITabBarControllerDelegate, but the method in the class does not been invoked.

AppDelegate.swift:

func application() {
  // ...

  let controller = MyTabItemController()
  let tabBarController = UITabBarController()
  tabBarController.viewControllers = [controller]
  tabBarController.delegate = MyTabBarControllerDelegate()

  self.window?.rootViewController = tabBarController

  // ...
}

MyTabBarControllerDelegate.swift:

class MyTabBarControllerDelegate: NSObject, UITabBarControllerDelegate{
  func tabBarController(/*...*/) {
    print("method invoked")
  }
}

When I selected the item, the message "method invoked" is not shown.

If I let AppDelegate extend UITabBarControllerDelegate, everything worked well and the message is shown in console.

I want to know why this happened?

1
  • Any particular reason for creating a separate class for tabbarController delegate ? Commented Feb 26, 2018 at 7:23

1 Answer 1

1

The delegate property of UITabBarController is weak. Thus, your delegate is released directly after the assignment. You should keep you delegate object by a strong reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.