IOS Swift以编程方式创建的navigationController TOOLBAR(底栏)项目操作不起作用
Posted
技术标签:
【中文标题】IOS Swift以编程方式创建的navigationController TOOLBAR(底栏)项目操作不起作用【英文标题】:IOS Swift Programmatically created navigationController TOOLBAR (bottom bar) item actions not working 【发布时间】:2017-01-08 03:05:21 【问题描述】:我无法以编程方式创建 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 是测试(以编程方式创建):)
【问题讨论】:
仅供参考 - 您只需要一个灵活的空间。它可以在同一个工具栏中多次使用。 感谢您的提示。我假设不是因为视图控制器子视图列表通常在故事板中的外观。 【参考方案1】:首先,您必须确保您的ProgOneViewController
确实包含在UINavigationController
中。如果是这种情况,则显示工具栏和 barbuttonitems(在我的项目中尝试了您的代码)。
除此之外,您必须将所有 barbuttonitem 声明更改为 lazy var
,以便声明中对 self
的引用指向视图控制器和目标操作。
如果有任何问题,请随时询问:)
【讨论】:
谢谢!对此,我真的非常感激。而且我确实将 viewController 嵌入到导航控制器中。它是在另一个视图控制器中声明的。 lazy 关键字是我需要研究的。但再次感谢!以上是关于IOS Swift以编程方式创建的navigationController TOOLBAR(底栏)项目操作不起作用的主要内容,如果未能解决你的问题,请参考以下文章
使用 CoreData 以编程方式创建实体/表,并在 Swift 3 for iOS 中将值插入该实体
IOS Swift以编程方式创建的navigationController TOOLBAR(底栏)项目操作不起作用
ios swift方法以编程方式浏览viewcontroller