我有一个带 3 个 child 的 TabViewController(2 个 TableViewController 和一个带标签和 TextView 的 ViewController)
我已经在导航中添加了一个 RightBarButtonItem,但问题是条形按钮没有触发。
在一些实验之后,我注意到只有当所选 View 为ViewController时,并且只有在编辑 TextView 时,bar button dires dires。 如果 TextView 已完成编辑,或者所选 View 是 TableViewControllers 之一,则不会触发 Bar Button
这是我的 TabViewController 类
class InfoTabViewController: UITabBarController, UITabBarControllerDelegate {
var index: Int?
var rightBarItem: UIBarButtonItem?
override func viewDidLoad() {
super.viewDidLoad()
// MARK: RightBarButtonItem
rightBarItem = UIBarButtonItem()
rightBarItem!.style = .Plain
rightBarItem!.action = #selector(self.rightBarItemSelected)
self.navigationItem.rightBarButtonItem = rightBarItem
self.delegate = self
}
override func viewDidAppear(animated: Bool) {
// MARK: RightBarButtonItem
editRightBarItem()
}
//Useless, fires before the tabbar item is selected not after... go to TabBarController instead
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
//print(self.selectedViewController?.title)
}
// UITabBarControllerDelegate fires after the tabbar item is selected, good one
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
editRightBarItem()
}
// MARK: RightBarButtonItem
func editRightBarItem() {
if let title = self.selectedViewController?.title {
switch title {
case "Tab Investigations":
rightBarItem!.title = "Add"
//rightBarItem!.enabled = true
break
case "Tab Treatment":
rightBarItem!.title = "Add"
//rightBarItem!.enabled = true
break
case "Tab Images":
rightBarItem!.title = "Add"
//rightBarItem!.enabled = true
break
case "Tab Notes":
rightBarItem!.title = "Save"
//rightBarItem!.enabled = true
break
default:
break
}
}
}
// MARK: RightBarButtonItem
func rightBarItemSelected() {
print("right bar item selected")
if let title = self.selectedViewController?.title {
print(title)
switch title {
case "Tab Investigations":
getInput(title, title: "New Test", message: "Insert Name and Value")
break
case "Tab Treatment":
getInput(title, title: "New Drug", message: "Insert Name and Dose")
break
case "Tab Images":
// do something
break
case "Tab Notes":
let view = self.viewControllers![0] as! NotesViewController
Patient.lstPatients[index!].history = view.textboxNotes.text
view.textboxNotes.endEditing(true)
//getInput("Add new test", message: "Insert name and value", viewTitle: self.selectedViewController!.title!)
break
default:
break
}
}
}
最佳答案
按钮不触发的原因 - 您忘记设置 target
。以下是文档中的一些话:
weak var target: AnyObject? { get set }
The object that receives an action when the item is selected. If nil, the action message is passed up the responder chain where it may be handled by any object implementing a method corresponding to the selector held by the action property. The default value is nil.
所以,尝试添加
rightBarItem.target = self
或者使用更宽的初始化器
rightBarItem = UIBarButtonItem(title: "Add", style: .Plain, target: self, action: #selector(self.rightBarItemSelected))
关于ios - 除非正在编辑 subview 内的 textView,否则 TabViewController 中的 RightBarItem 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38240814/