如何在 swift 3 中使用 addTarget 方法



这是我的button对象

    let loginRegisterButton:UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = UIColor(r: 50 , g: 80, b: 130)
    button.setTitle("Register", for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitleColor(.white, for: .normal)
    button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside)
    return button
}()

这是我的函数

    func handleRegister(){
    FIRAuth.auth()?.createUser(withEmail: email, password: password,completion: { (user, error) in
        if error != nil
        { print("Error Occured")}
        else
        {print("Successfully Authenticated")}
    })        
}

我收到编译错误,如果删除了addTarget,它编译成功

是的

,如果没有参数,请不要添加"((">

button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside). 

如果你想得到发件人

button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside). 
func handleRegister(sender: UIButton){
   //...
}

编辑:

button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside)

不再有效,您需要将选择器中的_替换为您在函数标头中使用的变量名称,在这种情况下它将是 sender ,因此工作代码变为:

button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)

试试 Swift 4

buttonSection.addTarget(self, action: #selector(actionWithParam(_:)), for: .touchUpInside)
@objc func actionWithParam(sender: UIButton){
    //...
}
buttonSection.addTarget(self, action: #selector(actionWithoutParam), for: .touchUpInside)
@objc func actionWithoutParam(){
    //...
}

试试这个

button.addTarget(self, action:#selector(handleRegister()), for: .touchUpInside). 

只需添加带有方法名称的括号即可。

您也可以参考链接:类型为"自定义按钮"的值没有成员"触地">

  let button: UIButton = UIButton()
    button.setImage(UIImage(named:"imagename"), for: .normal)
    button.addTarget(self, action:#selector(YourClassName.backAction(_sender:)), for: .touchUpInside)
    button.frame = CGRect.init(x: 5, y: 100, width: 45, height: 45)
    view.addSubview(button)
    @objc public func backAction(_sender: UIButton) {
    }

尝试使用 swift 3

cell.TaxToolTips.tag = indexPath.row
        cell.TaxToolTips.addTarget(self, action: #selector(InheritanceTaxViewController.displayToolTipDetails(_:)), for:.touchUpInside)

 @objc func displayToolTipDetails(_ sender : UIButton) {
        print(sender.tag)
        let tooltipString = TaxToolTipsArray[sender.tag]
        self.displayMyAlertMessage(userMessage: tooltipString, status: 202)    
}

在 swift 3 中使用这个 -

object?.addTarget(objectWhichHasMethod, action: #selector(classWhichHasMethod.yourMethod), for: someUIControlEvents)

例如(从我的代码中(-

self.datePicker?.addTarget(self, action:#selector(InfoTableViewCell.datePickerValueChanged), for: .valueChanged)

如果您希望将发送方作为参数,只需在方法名称后给出一个:

试试 Swift 3

button.addTarget(self, action:#selector(ClassName.handleRegister(sender:)), for: .touchUpInside)

祝你好运!

海报从 9 月 21 日开始的第二条评论是正确的。 对于那些以后可能会遇到与海报相同的问题的人,这里有一个简短的解释。 其他答案最好记住,但不能解决此代码遇到的常见问题。

在 Swift 中,使用 let 关键字做出的声明是常量。 当然,如果要将项添加到数组中,则不能将数组声明为常量,但分段控件应该没问题,对吧?! 如果在声明中引用已完成的分段控件,则不会。

引用对象(在本例中为UISegmentedControl,但当您说.addTarget并让目标self时,也会发生UIButton(,事情会崩溃。 为什么? 因为self正处于被定义的过程中。 但我们确实希望将行为定义为对象的一部分......用 var惰地将其声明为变量。 lazy欺骗编译器,使其认为self是明确定义的 - 它使您的编译器在声明时保持沉默。 延迟声明的变量在首次调用之前不会设置。 所以在这种情况下,lazy 允许您在设置对象时毫无问题地使用self的概念,然后当您的对象获得.touchUpInside.valueChanged.addTarget()中的任何第 3 个参数时,然后它调用self的概念,此时它已完全建立并完全准备好成为有效目标。 因此,它可以让您懒惰地声明变量。 在这种情况下,我认为他们可以给我们一个像 necessary 这样的关键字,但它通常被视为一种懒惰、草率的做法,您不想在您的代码中全部使用它,尽管它可能在这种情况下占有一席之地。 它是什么

在 Swift 中没有lazy let(常量没有lazy(。

这是关于懒惰的苹果文档。

这是关于变量和常量的苹果。 在声明下的语言参考中还有更多内容。

而不是

let loginRegisterButton:UIButton = {
//...  }()

尝试:

lazy var loginRegisterButton:UIButton = {
//...  }()

这应该可以修复编译错误!!

Demo from Apple 文档。 https://developer.apple.com/documentation/swift/using_objective-c_runtime_features_in_swift

import UIKit
class MyViewController: UIViewController {
    let myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // without parameter style
        let action = #selector(MyViewController.tappedButton)
        // with parameter style
        // #selector(MyViewController.tappedButton(_:))
        myButton.addTarget(self, action: action, forControlEvents: .touchUpInside)
    }
    @objc func tappedButton(_ sender: UIButton?) {
        print("tapped button")
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}

最新更新