首页 > 解决方案 > 从 UIButton 更改后的 UIBarButtonItem 错误

问题描述

我遇到了这个问题,我得到了一些示例代码,我试图用UIBarButtonItems.

谁能说出我的代码中的问题是什么,我不知道普通的按钮和UIBarButtonItems不同的按钮。

所以我将 showPopup 中的发件人从 更改为UIViewUIButton但现在出现另一个错误(请参见下面的屏幕截图)

错误消息和代码的屏幕截图

很抱歉发布代码的图像,但没有发布代码。继承人的shopPopup功能:

func showPopup(sender: UIButton, mode:String, text:String="", row:Int=0) {
if !(mode=="edit" || mode=="new") { return }

let popVC = storyboard?.instantiateViewController(withIdentifier: "NewPopup") as! NewPopupVC
popVC.mode = mode
popVC.currentText = text
popVC.currentRow = row
popVC.modalPresentationStyle = .popover

let popPC = popVC.popoverPresentationController!
popPC.sourceView = sender
popPC.sourceRect = sender.bounds
popPC.delegate = self
popPC.permittedArrowDirections = [.up, .down]

present(popVC, animated:true, completion: nil) 
}

继承人 func addButton。这就是第一个错误所在。

@IBAction func addButton(_ sender: UIBarButtonItem) {
    showPopup(sender: sender, mode: "new")
}

这是发生第二个错误的部分。

@objc func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
if gesture.state != .began { return }

let pt = gesture.location(in: tableView)
if let path = tableView.indexPathForRow(at: pt),
   let row = (path as NSIndexPath?)?.row,
   let cell = tableView.cellForRow(at: path)
{

  showPopup(sender: cell,
            mode: "edit",
            text: todoList[row],
            row: row)
}
}

更新了最后出现的错误的屏幕截图。 截屏

标签: swift

解决方案


你必须这样做

 func showPopup(sender: UIButton, mode: String, text:String = "", row:Int = 0)

sender从更改UIViewUIButton


推荐阅读