Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to create navigation barButtonItem #42

Closed
Sophie1214 opened this issue Aug 14, 2018 · 8 comments
Closed

Failed to create navigation barButtonItem #42

Sophie1214 opened this issue Aug 14, 2018 · 8 comments

Comments

@Sophie1214
Copy link

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
Copy link
Owner

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

@Sophie1214
Copy link
Author

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
Copy link
Owner

Do you mind sharing the entire swift class?

@Sophie1214
Copy link
Author

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
Copy link
Owner

@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
Copy link
Author

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

@Minitour
Copy link
Owner

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

@Sophie1214
Copy link
Author

Yes it worked !! Thank you<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants