IOS Swift 以编程方式创建的 navigationController TOOLBAR(底栏)项目操作不起作用

IOS Swift Programmatically created navigationController TOOLBAR (bottom bar) item actions not working

我在以编程方式创建 UINavigationController 工具栏时遇到问题。我以前曾使用故事板成功地做到这一点,但我想尝试用代码来完成这一切。

我已经以编程方式创建了 UIBarButtonItems,但是它们在按下时应该执行的实际功能或操作不起作用。

澄清一下,我并没有尝试将 UIBarButtonItems 添加到顶部栏。我见过几十个问题都在问同样的事情。我指的是 UINavigationController 底部的工具栏。这意味着没有 "rightBarButtonItem" 或 "leftBarButtonItem"

代码如下:

class ProgOneViewController: UIViewController {
    var chatRoomsButton: UIBarButtonItem = {
        var button = UIBarButtonItem(title: "Chats", style: .plain, target: self, action: #selector(segueToChatRoomController(_:)))
        return button
    }()

    var exploreButton: UIBarButtonItem = {
        var button = UIBarButtonItem(title: "Explore", style: .plain, target: self, action: #selector(segueToExploreController(_:)))
        return button
    }()

    var profileButton: UIBarButtonItem = {
        var button = UIBarButtonItem(title: "Profile", style: .plain, target: self, action: #selector(segueToProfileController(_:)))

        return button
    }()

    // Flexible spaces that are added in between each button.
    var flexibleSpace1: UIBarButtonItem = {
        var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        return flexibleSpace
    }()

    var flexibleSpace2: UIBarButtonItem = {
        var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        return flexibleSpace
    }()

    var flexibleSpace3: UIBarButtonItem = {
        var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        return flexibleSpace
    }()

    var flexibleSpace4: UIBarButtonItem = {
        var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        return flexibleSpace
    }()

    // These are the functions that are not being called for some mysterious reason. 
    func segueToChatRoomController(_ sender: Any) {
        print("segueing to chat rooms controller")
        let chatRoomsController = ChatRoomsViewController()
        let navController = UINavigationController(rootViewController: chatRoomsController)
        self.present(navController, animated: false, completion: nil)
    }

    func segueToExploreController(_ sender: Any) {
        print("segueing to explore controller")
        let exploreController = ExploreCollectionViewController()
        let navController = UINavigationController(rootViewController: exploreController)
        self.present(navController, animated: false, completion: nil)
    }

    func segueToProfileController(_ sender: Any) {
        print("segueing to profile controller")
        let profileController = ProfileTableViewController()
        let navController = UINavigationController(rootViewController: profileController)
        self.present(navController, animated: false, completion: nil)
    }

    func setUpToolbar() {
        print("setting up toolbar")

        self.navigationController?.setToolbarHidden(false, animated: false)
        self.navigationController?.toolbar.isUserInteractionEnabled = true

        let toolBarItems = [flexibleSpace1, chatRoomsButton, flexibleSpace2, exploreButton, flexibleSpace3, profileButton, flexibleSpace4]

        self.setToolbarItems(toolBarItems, animated: true)
        // For some reason, these two methods leave the toolbar empty.

        //self.navigationController?.setToolbarItems(toolBarItems, animated: true)

        //self.navigationController?.toolbar.items = toolBarItems
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        setUpToolbar()
    }

 }

编辑* 这是用于在 UINavigationController

中实例化 ProgOneViewController 的代码
 func pushToTestProgrammaticallyCreatedViews() {
    let progOneViewController = ProgOneViewController()
    let navController = UINavigationController(rootViewController: progOneViewController)
    //navController.isToolbarHidden = false
    //progOneViewController.setUpToolbar()

    self.present(navController, animated: false, completion: nil)
}

我在故事板创建的视图控制器中单击按钮时调用了此函数。函数签名很长,以便指定哪些 viewController 是测试(以编程方式创建):)

首先你必须确保你的 ProgOneViewController 确实包含在 UINavigationController 中。如果是这种情况,则会显示工具栏和栏按钮项(在我的项目中尝试过您的代码)。

除此之外,您必须将所有 barbuttonitem 声明更改为 lazy var,以便声明中对 self 的引用指向 viewcontroller 和目标操作。

有什么问题欢迎随时提问:)