我有一个UIBarButtonItem:
let addProdButton = UIBarButtonItem(title: "+", style: UIBarButtonItemStyle.plain, target: self, action: #selector(AddProduct))
与之相关的行动:
@objc func AddProduct()
{
performSegue(withIdentifier: "segue_to_add_product", sender: self)
}
我将它添加到我的navigationBar:
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])
}
我的SetSearchBar功能:
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
}
答案 0 :(得分:1)
您提供的代码应该正常工作,因此您需要提供更多上下文(显示更多视图控制器类的实现方式)。
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")
}
}