UIBarButtonItem 没有启动选择器操作
UIBarButtonItem not firing up selector action
我有一个 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 未启动)。我如何让它发挥作用?
更多问题:
- 如何为我的 UITabBarButton 添加边框?
- swift 中是否有通用的 "Add" 按钮可供我使用? (或者只是 stye/picture)
- 如何放大按钮内的文字?
编辑:
我刚刚注意到按钮可以点击一次,然后点击动画就不再显示了。
我的 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
}
您提供的代码应该可以正常工作,因此您需要提供更多上下文(显示更多实现此功能的视图控制器 class)。
UIKit 提供了一组称为系统项的标准 UIBarButtonItems。这些恰好包括一个 'add' 项,显示为 plus-sign 并在整个 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")
}
}
我有一个 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 未启动)。我如何让它发挥作用?
更多问题:
- 如何为我的 UITabBarButton 添加边框?
- swift 中是否有通用的 "Add" 按钮可供我使用? (或者只是 stye/picture)
- 如何放大按钮内的文字?
编辑:
我刚刚注意到按钮可以点击一次,然后点击动画就不再显示了。
我的 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
}
您提供的代码应该可以正常工作,因此您需要提供更多上下文(显示更多实现此功能的视图控制器 class)。
UIKit 提供了一组称为系统项的标准 UIBarButtonItems。这些恰好包括一个 'add' 项,显示为 plus-sign 并在整个 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")
}
}