![](/img/trans.png)
[英]Why “(UIButton *)sender” and not “UIButton *sender”?
[英]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
但無法正常工作。 你有什么主意嗎?
您可以將該參數設為可選:
@IBAction func addProductToCartButton(_ sender: UIButton?)
{
// Do your stuff here
}
現在您可以這樣稱呼它:
addProductToCartButton(nil)
如果您不想將參數設為可選,則可以這樣調用它:
addProductToCartButton(UIButton()) // It's not recommended
只需編寫另一個實用程序函數,然后在其中添加代碼(將IBAction內部編寫的代碼添加到該函數中)。 而不是從另一個函數調用IBAction
,而是調用此實用程序函數。
您需要重構代碼。 addProductToCartButton
的當前實現使用發送者(按鈕)來確定索引路徑。 然后其余的代碼基於該索引路徑。
然后,您didTapAddToCart
方法,該方法嘗試調用addProductToCartButton
但是此時您沒有按鈕,但是它確實具有索引路徑。
我將創建一個新函數,將索引路徑作為其參數。 它的實現是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)
}
擴展Midhun MP
的答案,您可以通過提供默認值nil
來使函數調用更加簡單:
@IBAction func addProductToCartButton(_ sender: UIButton? = nil) {
// Do your stuff here
}
然后,您可以像下面這樣調用函數:
addProductToCartButton()
這種邏輯最簡單的方法是,將nil值作為參數傳遞,就像這樣,
addProductToCartButton(nil)
只要確保您沒有在函數中使用任何按鈕屬性即可。 但是,如果您使用的是button屬性,那么只需在函數中添加一個檢查,就像這樣,
func addProductToCartButton(_ sender: UIButton) {
if sender != nil {
//Do Something
}
}
希望這能解決您的問題,謝謝閱讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.