ios - 除非正在编辑 subview 内的 textView,否则 TabViewController 中的 RightBarItem 不会触发

标签 ios xcode swift

我有一个带 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/

相关文章:

xcode - 在 Runner.xcodeproj/project.pbxproj 中硬编码 FLUTTER_ROOT

ios - JSONSerialization.jsonObject

IOS 发布问题(无效的 Swift 支持)

iphone - 将 vcard 的文件类型与 iPhone 应用程序相关联

ios - UITableViewCell detailTextLabel 从不显示

arrays - 在 swift 中创建一个多维数组并使用数据填充 3 个 TableView

ios - 缩放超过 ImageView 边界

ios - 获取 UINavigationController 向后滑动的进度

ios - UIDocumentPickerViewController 按钮看起来已禁用但有效 - 为什么?

iOS Swift - CoreBluetooth 无法发送数据