我有一个UIBarButtonItem:
let addProdButton = UIBarButtonItem(title: "+", style: UIBarButtonItemStyle.plain, target: self, action: #selector(AddProduct))
它与之相关的行动:
@objc func AddProduct()
{
performSegue(withIdentifier: "segue_to_add_product", sender: self)
}
我将它添加到我的导航栏:
addProdButton.isEnabled = true
self.navigationItem.leftBarButtonItem = addProdButton
当我启动应用程序时,它显示并可点击:
当我单击它时,它可以作为可点击的,但由于某种原因,它不会启动我创建它的动作(断点没有被击中,segue没有被激活)。我如何使其工作?
更多问题:
编辑:
我刚注意到该按钮可以点击ONCE,然后点击动画不再显示。
我的viewDidLoad函数:
override func viewDidLoad()
{
super.viewDidLoad()
SetActivityIndicator()
SetSearchBar()
addProdButton.isEnabled = true
self.navigationItem.leftBarButtonItem = addProdButton
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 200, height: 250)
self.ProductsCollection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
ProductsCollection.dataSource = self
ProductsCollection.delegate = self
ProductsCollection.register(ProductsCollectionViewCell.self, forCellWithReuseIdentifier: "product_collection_cell")
ProductsCollection.backgroundColor = UIColor.clear
let topConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 20)
let bottomConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -50) //leaving space for search field
let leadingConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)
ProductsCollection.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(ProductsCollection)
self.view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
}
我的设置搜索栏功能:
internal func SetSearchBar()
{
self.productsSearchController.searchResultsUpdater = self
self.productsSearchController.delegate = self
self.productsSearchController.searchBar.delegate = self
self.productsSearchController.hidesNavigationBarDuringPresentation = false
self.productsSearchController.dimsBackgroundDuringPresentation = true
self.productsSearchController.obscuresBackgroundDuringPresentation = false
productsSearchController.searchBar.placeholder = "Search for tools and resources"
productsSearchController.searchBar.sizeToFit()
productsSearchController.searchBar.becomeFirstResponder()
self.navigationItem.titleView = productsSearchController.searchBar
}
您提供的代码应该正常工作,因此您需要提供更多上下文(显示更多的视图控制器类,以实现此目的)。
UIKit提供了一组称为系统项的标准UIBarButtonItems。这些恰好包括一个“添加”项目,该项目显示为加号并在整个iOS中使用。使用此项解决了问题2和3。
UIBarButtonItem具有用于初始化系统项的便利构造函数。请参阅下面的工作示例:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let addProdButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addProduct))
navigationItem.leftBarButtonItem = addProdButton
}
@objc func addProduct() {
print("addProduct() called")
}
}