如何忽略发送者参数 UIButton
How to ignore the sender parameter UIButton
我曾将一个函数用作 @IBAction
,但现在我想将其用作普通函数。但问题是当我尝试调用函数时它要求我提供发件人并期望 UIButton
作为参数。
我怎样才能删除那个发件人,以免影响我的功能?
这是我的函数:
func addProductToCartButton(_ sender: UIButton) {
// Start animation region
let buttonPosition : CGPoint = sender.convert(sender.bounds.origin, to: self.productsTableView)
let indexPath = self.productsTableView.indexPathForRow(at: buttonPosition)!
let cell = productsTableView.cellForRow(at: indexPath) as! ProductTableViewCell
let imageViewPosition : CGPoint = cell.productImageView.convert(cell.productImageView.bounds.origin, to: self.view)
let imgViewTemp = UIImageView(frame: CGRect(x: imageViewPosition.x, y: imageViewPosition.y, width: cell.productImageView.frame.size.width, height: cell.productImageView.frame.size.height))
imgViewTemp.image = cell.productImageView.image
animationProduct(tempView: imgViewTemp)
// End animation region
}
这里是我需要调用函数的地方:
func didTapAddToCart(_ cell: ProductTableViewCell) {
let indexPath = self.productsTableView.indexPath(for: cell)
addProductToCartButton( expecting UIBUTTON parameter)
}
我试图将发件人设置为 nil
,但没有成功。你有什么想法吗?
方法 1(推荐):
您可以将该参数设为可选:
@IBAction func addProductToCartButton(_ sender: UIButton?)
{
// Do your stuff here
}
现在您可以这样称呼它:
addProductToCartButton(nil)
方法 2(不推荐)
如果你不想让参数成为可选参数,你可以这样称呼它:
addProductToCartButton(UIButton()) // It's not recommended
方法 3(推荐)
再写一个实用函数,把代码加进去(把IBAction里面写的代码加到这个函数里)。不要从另一个函数调用 IBAction
,而是调用这个实用函数。
扩展 Midhun MP
的答案,您可以通过提供默认值 nil
:
使您的函数调用更加简单
@IBAction func addProductToCartButton(_ sender: UIButton? = nil) {
// Do your stuff here
}
然后你可以这样调用函数:
addProductToCartButton()
这种逻辑最简单的方法是,将零值作为参数传递,就像这样,
addProductToCartButton(nil)
只需确保您没有在函数中使用任何按钮 属性。但是,如果您使用的是按钮 属性,那么只需像这样简单地在您的函数中添加一个检查,
func addProductToCartButton(_ sender: UIButton) {
if sender != nil {
//Do Something
}
}
希望这能解决您的问题,感谢阅读。
您需要重构您的代码。 addProductToCartButton
的当前实现使用发件人(按钮)来确定索引路径。然后其余代码基于该索引路径。
然后您的 didTapAddToCart
方法尝试调用 addProductToCartButton
但此时您没有按钮,但它确实有一个索引路径。
我将创建一个以索引路径作为参数的新函数。它的实现是addProductToCartButton
.
中的大部分现有代码
这是新函数(主要是原始 addProductToCartButton
代码):
func addProduct(at indexPath: IndexPath) {
let cell = productsTableView.cellForRow(at: indexPath) as! ProductTableViewCell
let imageViewPosition : CGPoint = cell.productImageView.convert(cell.productImageView.bounds.origin, to: self.view)
let imgViewTemp = UIImageView(frame: CGRect(x: imageViewPosition.x, y: imageViewPosition.y, width: cell.productImageView.frame.size.width, height: cell.productImageView.frame.size.height))
imgViewTemp.image = cell.productImageView.image
animationProduct(tempView: imgViewTemp)
// End animation region
}
然后重做 addProductToCartButton
为:
func addProductToCartButton(_ sender: UIButton) {
// Start animation region
let buttonPosition : CGPoint = sender.convert(sender.bounds.origin, to: self.productsTableView)
let indexPath = self.productsTableView.indexPathForRow(at: buttonPosition)!
addProduct(at: indexPath)
}
最后,更新 didTapAddToCart
:
func didTapAddToCart(_ cell: ProductTableViewCell) {
let indexPath = self.productsTableView.indexPath(for: cell)
addProduct(at: indexPath)
}
我曾将一个函数用作 @IBAction
,但现在我想将其用作普通函数。但问题是当我尝试调用函数时它要求我提供发件人并期望 UIButton
作为参数。
我怎样才能删除那个发件人,以免影响我的功能?
这是我的函数:
func addProductToCartButton(_ sender: UIButton) {
// Start animation region
let buttonPosition : CGPoint = sender.convert(sender.bounds.origin, to: self.productsTableView)
let indexPath = self.productsTableView.indexPathForRow(at: buttonPosition)!
let cell = productsTableView.cellForRow(at: indexPath) as! ProductTableViewCell
let imageViewPosition : CGPoint = cell.productImageView.convert(cell.productImageView.bounds.origin, to: self.view)
let imgViewTemp = UIImageView(frame: CGRect(x: imageViewPosition.x, y: imageViewPosition.y, width: cell.productImageView.frame.size.width, height: cell.productImageView.frame.size.height))
imgViewTemp.image = cell.productImageView.image
animationProduct(tempView: imgViewTemp)
// End animation region
}
这里是我需要调用函数的地方:
func didTapAddToCart(_ cell: ProductTableViewCell) {
let indexPath = self.productsTableView.indexPath(for: cell)
addProductToCartButton( expecting UIBUTTON parameter)
}
我试图将发件人设置为 nil
,但没有成功。你有什么想法吗?
方法 1(推荐):
您可以将该参数设为可选:
@IBAction func addProductToCartButton(_ sender: UIButton?)
{
// Do your stuff here
}
现在您可以这样称呼它:
addProductToCartButton(nil)
方法 2(不推荐)
如果你不想让参数成为可选参数,你可以这样称呼它:
addProductToCartButton(UIButton()) // It's not recommended
方法 3(推荐)
再写一个实用函数,把代码加进去(把IBAction里面写的代码加到这个函数里)。不要从另一个函数调用 IBAction
,而是调用这个实用函数。
扩展 Midhun MP
的答案,您可以通过提供默认值 nil
:
@IBAction func addProductToCartButton(_ sender: UIButton? = nil) {
// Do your stuff here
}
然后你可以这样调用函数:
addProductToCartButton()
这种逻辑最简单的方法是,将零值作为参数传递,就像这样,
addProductToCartButton(nil)
只需确保您没有在函数中使用任何按钮 属性。但是,如果您使用的是按钮 属性,那么只需像这样简单地在您的函数中添加一个检查,
func addProductToCartButton(_ sender: UIButton) {
if sender != nil {
//Do Something
}
}
希望这能解决您的问题,感谢阅读。
您需要重构您的代码。 addProductToCartButton
的当前实现使用发件人(按钮)来确定索引路径。然后其余代码基于该索引路径。
然后您的 didTapAddToCart
方法尝试调用 addProductToCartButton
但此时您没有按钮,但它确实有一个索引路径。
我将创建一个以索引路径作为参数的新函数。它的实现是addProductToCartButton
.
这是新函数(主要是原始 addProductToCartButton
代码):
func addProduct(at indexPath: IndexPath) {
let cell = productsTableView.cellForRow(at: indexPath) as! ProductTableViewCell
let imageViewPosition : CGPoint = cell.productImageView.convert(cell.productImageView.bounds.origin, to: self.view)
let imgViewTemp = UIImageView(frame: CGRect(x: imageViewPosition.x, y: imageViewPosition.y, width: cell.productImageView.frame.size.width, height: cell.productImageView.frame.size.height))
imgViewTemp.image = cell.productImageView.image
animationProduct(tempView: imgViewTemp)
// End animation region
}
然后重做 addProductToCartButton
为:
func addProductToCartButton(_ sender: UIButton) {
// Start animation region
let buttonPosition : CGPoint = sender.convert(sender.bounds.origin, to: self.productsTableView)
let indexPath = self.productsTableView.indexPathForRow(at: buttonPosition)!
addProduct(at: indexPath)
}
最后,更新 didTapAddToCart
:
func didTapAddToCart(_ cell: ProductTableViewCell) {
let indexPath = self.productsTableView.indexPath(for: cell)
addProduct(at: indexPath)
}