Minitour / AZTabBarController

A custom tab bar controller for iOS written in Swift 4.2
MIT License
348 stars 65 forks source link

Failed to create navigation barButtonItem #42

Closed Sophie1214 closed 6 years ago

Sophie1214 commented 6 years ago

I tried to create a UIBarButtonItem by using

controller.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "xxx", style: .plain, target: self, action:xxx)

But the item failed to be created, and I tried with both the AZTabBarController and one of the tab controllers.

A little help would be much appreciated~

Minitour commented 6 years ago

@Sophie1214 Hi, can you please describe what are you trying to achieve visually speaking?

Sophie1214 commented 6 years ago

Okay, so I created an empty UIViewController in storyboard, which is the 'parent' below, and it is embedded inside a navigation controller.

let tabController = AZTabBarController.insert(into: parent, withTabIconNames: icons)

So now the tabController is created with a navigation bar.

I want to create a Save button at the top right corner of the navigation bar, when the first tab is selected OR when any of the tab is selected.

Minitour commented 6 years ago

Do you mind sharing the entire swift class?

Sophie1214 commented 6 years ago

Sure, but it might be a little tedious.

Insert the AZTabBarController into the navigation stack:

if let navigationController = navigationController {
            var viewControllers = navigationController.viewControllers

            let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let parentController = storyBoard.instantiateViewController(withIdentifier: "TabRootController")
            Util.createTabController(storyBoard: storyBoard, parent: parentController, asset: self.asset)

            viewControllers.append(parentController)
            navigationController.setViewControllers(viewControllers, animated: true)
        }

Shortened 'createTabController' method:

static func createTabController(storyBoard: UIStoryboard, parent: UIViewController, asset: Asset?){
       ...
        let tabController = AZTabBarController.insert(into: parent, withTabIconNames: icons)
        let dashboard = storyBoard.instantiateViewController(withIdentifier: "dashboard") as! DashboardViewController
       ...
        tabController.setViewController(dashboard, atIndex: 0)
        tabController.setViewController(status, atIndex: 1)
        tabController.setViewController(logs, atIndex: 2)
        tabController.setViewController(profile, atIndex: 3)
  ...
        tabController.highlightButton(atIndex: 0)
        tabController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action:nil)
    }

Dashboard View Controller:

class DashboardViewController: UIViewController{
...
override func viewDidLoad() {
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(save))
    }
...
Minitour commented 6 years ago

@Sophie1214

So if I understood your code correctly, You are trying to insert the Tab Bar into a controller which is loaded from a storyboard, then setting that controller as the root controller of the existing navigation controller. You then proceed into loading another controller from storyboard and setting it as one of the tabs of the tab bar.

Alright I think I understand the issue.

Try setting the navigation item of the tab bar instead of the of the child view controller that is set in the tab bar.

for example:

class DashboardViewController: UIViewController{
...
override func viewDidLoad() {
        let tabBar = self.currentTabBar
        tabBar?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(save))
    }
...

Let me know if that solves the issue.

Sophie1214 commented 6 years ago

Unfortunately it didn't solve the issue, but you understood the setup perfectly~

Minitour commented 6 years ago

Alright try one last thing: try instead of tabBar? tabBar?.parentViewController?

Sophie1214 commented 6 years ago

Yes it worked !! Thank you<3