Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:目标未在子类UIButton上被调用_Ios_Swift_Uibutton_Subclass_Addtarget - Fatal编程技术网

Ios Swift:目标未在子类UIButton上被调用

Ios Swift:目标未在子类UIButton上被调用,ios,swift,uibutton,subclass,addtarget,Ios,Swift,Uibutton,Subclass,Addtarget,斯威夫特 class CustomButton: UIButton { override func draw(_ rect: CGRect) { //drawing code } } ViewController.swift let testCustom = CustomButton() testCustom.draw(CGRect(x: 0, y: 0, width: 0, height: 0)) testCustom.isUserInteractionEn

斯威夫特

class CustomButton: UIButton {

    override func draw(_ rect: CGRect) {
        //drawing code
    }
}
ViewController.swift

let testCustom = CustomButton()
testCustom.draw(CGRect(x: 0, y: 0, width: 0, height: 0))
testCustom.isUserInteractionEnabled = true
testCustom.addTarget(self, action: #selector(Start(_:)), for: .touchUpInside)
self.view.addSubview(testCustom)

@objc func Start(_ sender: CustomButton) {
    print("pressed start")
}
按钮出现在屏幕上,但按下按钮后不会调用该功能。你知道为什么吗

我还尝试了CustomButton.swift中的函数和addTarget代码,但也无法触发该函数


谢谢你的帮助

以下是如何在视图控制器(
UIViewController
)中实例化
UIButton
子类的简单示例。根据Swift 4.2进行测试

// Subclassing UIButton //
import UIKit

class MyButton: UIButton {
    var tintColor0: UIColor!
    var tintColor1: UIColor!
    var borderColor: UIColor!
    var backColor: UIColor!
    var cornerRadius: CGFloat!

    required init(frame: CGRect, tintColor0: UIColor, tintColor1: UIColor, borderColor: UIColor, backColor: UIColor, cornerRadius: CGFloat, titleString: String) {
        super.init(frame: frame)

        self.tintColor0 = tintColor0
        self.tintColor1 = tintColor1
        self.borderColor = borderColor
        self.backColor = backColor
        self.cornerRadius = cornerRadius
        self.setTitle(titleString, for: .normal)
    }

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

    override func draw(_ rect: CGRect) {
        super.draw(rect)

        self.setTitleColor(tintColor0, for: .normal)
        self.setTitleColor(tintColor1, for: .highlighted)
        self.layer.borderColor = borderColor.cgColor
        self.layer.cornerRadius = cornerRadius
        self.layer.borderWidth = 1.0
        self.layer.backgroundColor = backColor.cgColor
    }
}

// View controller //
import UIKit

class ViewController: UIViewController {
    // MARK: - Variables

    // MARK: - IBOutlet

    // MARK: - IBAction

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        let buttonRect = CGRect(x: 20.0, y: 160.0, width: 100.0, height: 32.0)
        let myButton = MyButton(frame: buttonRect, tintColor0: UIColor.black, tintColor1: UIColor.gray, borderColor: UIColor.orange, backColor: UIColor.white, cornerRadius: 8.0, titleString: "Hello")
        myButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        view.addSubview(myButton)
    }

    @objc func buttonTapped(_ sender: UIButton) {
        print("Hello!?")
    }
}

@穆罕默德·瓦卡斯巴蒂(MuhammadWaqasBhati)提出了一个很好的问题,询问有关框架的问题

我使用addSublayer在屏幕上绘制我创建的路径。我的错误是,我在draw()函数中设置了值,并用addSublayer添加了CAShapeLayer,但没有设置按钮的帧

即使绘制的图层是按钮的子图层,它也会显示在为图层提供的坐标和尺寸上,与“父”按钮的框架没有任何关系


按钮的边框可以是(0,0,0,0)或(0,0,100,100),在addSublayer中绘制的图像仍然可以是(250,200,75,80),因此可见图像将位于屏幕的一个点上,但实际按钮位于与其子层中可见内容无关的点上。

您在哪里设置按钮边框?对于按钮交互,它应该具有适当的高度和宽度。@objc func Start(uuSender:UIButton){}@ElTomato我使用UIButton和CustomButton作为函数的发送方参数进行了尝试,但没有成功:/n您是否可以用
let testCustom=UIButton()替换
let testCustom=UIButton()进行检查
然后看看这是否基本有效,无论如何,这不是您子类
UIButton
的方式。