简体   繁体   English

如何将addtarget设置为自定义UIView

[英]How to set addtarget to custom UIView

I have a custom UIView called Icons. 我有一个称为图标的自定义UIView。 Everything is working perfectly fine right now except one thing. 除了一件事,现在一切都运转良好。 The problem that i am having is being able to set userinteraction to be working or to either make the button i have in my class to be working. 我遇到的问题是能够设置用户交互正常工作,或者使我班上的按钮正常工作。 When I add a target to the button, i get an error that crashes everything and with the userinteraction to the view, i get no response. 当我向按钮添加目标时,我得到一个错误,该错误使所有内容崩溃,并且由于用户与视图的交互作用,我没有任何响应。

class Icons: UIView {
    let xUnit = UIScreen.mainScreen().bounds.width/20
    let yUnit = UIScreen.mainScreen().bounds.height/30
    var name: String = String()
    var image: UIImage = UIImage()
    let font = UIFont(name: "HelveticaNeue-Thin", size: 16.0)!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.userInteractionEnabled = true



    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addCustomView(name:String, image: UIImage ){
        let label: UILabel = UILabel()
        label.frame = CGRectMake(0, 1.5*xUnit, 3.4*xUnit, 2.7*xUnit)
        label.textAlignment = NSTextAlignment.Center
        label.font = font
        label.text = name
        self.addSubview(label)

        let btn: UIButton = UIButton()
        btn.frame=CGRectMake(0.3*xUnit, 0, 2.5*xUnit, 2.5*xUnit)
        btn.setImage(image, forState: .Normal)
        self.addSubview(btn)


    }
}

And this is the class I'm trying to get connect it with which is in a completely different class 这是我试图与之建立联系的班级,它是一个完全不同的班级

func tapSettings(tapGestureRecognizer: UITapGestureRecognizer){
    let touchPoint = tapGestureRecognizer.locationInView(self.view)
    if(CGRectContainsPoint(settings.frame, touchPoint)){
        print("hi")
    }
}

when I am trying to connect it to the button I say this: 当我尝试将其连接到按钮时,我这样说:

btn.addTarget(self,action: #selector(HomeScreen.tapSettings))

and when I try and connect it to the view I say this : 当我尝试将其连接到视图时,我这样说:

let settingsTouch = UITapGestureRecognizer(target: self, action: #selector(HomeScreen.tapSettings))
        settingsTouch.numberOfTapsRequired = 1
        settingsTouch.numberOfTouchesRequired = 1
        self.view.addGestureRecognizer(settingsTouch)

try this 尝试这个

    btn.addTarget(HomeScreen.self, action: #selector(HomeScreen.tapSettings(_:)), forControlEvents: UIControlEvents.TouchUpInside)

I hope this helps 我希望这有帮助

There are two problems in your code: 您的代码中有两个问题:

  1. You are setting the target of the button to self when you should be setting it to an instance of HomeScreen, if that's where the method you want to call is declared 当您应将按钮的目标设置为HomeScreen的实例时,如果将其声明为要调用的方法,则将按钮的目标设置为self

  2. The argument a button action method receives is not UITapGestureRecognizer , but the clicked button instead, so you should replace your tapSettings method with something like this: 按钮操作方法接收的参数不是UITapGestureRecognizer ,而是单击的按钮,因此您应该使用以下内容替换tapSettings方法:

    func tapSettings(button: UIButton){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM