简体   繁体   English

UIView扩展内的UIButton

[英]UIButton inside UIView extension

I was wondering how I could extend an UIView and handle the target selector of an UIButton inside the extension function, but I'm not getting it right. 我想知道如何扩展UIView并在扩展函数中处理UIButton的目标选择器,但我做得不好。 It simply doesn't respond to the target selector. 它只是不响应目标选择器。 I've even tried using a proxy disclosure. 我什至尝试使用代理人披露。 Here's the code. 这是代码。

extension UIView { 扩展UIView {

class ClosureDispatch {

    let action: () -> ()

    init(f:() -> ()) {
        self.action = f
    }

    func execute() -> () {
        action()
    }

}


func showMessageView(inView: UIView) {


    var isShowing = false;

    let height: CGFloat = 80.0;
    let paddingBottom: CGFloat = 20.0;


    var backgroundView: UIView  = UIView(frame: CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height))
    backgroundView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.8);

    let close: ClosureDispatch = ClosureDispatch(f: {

        NSLog("go!")

        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

            backgroundView.frame = CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height)

            }) { (completed) -> Void in

                isShowing = false;

        }
    })


    var closeButton: UIButton = UIButton(frame: CGRectMake(inView.frame.size.width-height-paddingBottom, 0, height, height))
    closeButton.setTitle("Close", forState: .Normal)
    closeButton.addTarget(close, action: "execute", forControlEvents: .TouchUpInside)
    closeButton.backgroundColor = UIColor.blueColor()

    closeButton.userInteractionEnabled = true;
    backgroundView.userInteractionEnabled = true;

    backgroundView.addSubview(closeButton)
    inView.addSubview(backgroundView)

    UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

        backgroundView.frame = CGRectMake(0, inView.frame.size.height-height-paddingBottom, inView.frame.size.width, height)

    }) { (completed) -> Void in

        isShowing = true;

        UIView.animateWithDuration(0.5, delay: 3.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

            backgroundView.frame = CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height)

            }) { (completed) -> Void in

                isShowing = false;
        }
    }


}

} }

Try adding @objc in front of the execute method ie @objc func execute()... 尝试在execute方法前面添加@objc ,即@objc func execute()...

See this link . 请参阅此链接

If your Swift class inherits from an Objective-C class, all of the methods and properties in the class are available as Objective-C selectors. 如果您的Swift类是从Objective-C类继承的,则该类中的所有方法和属性都可用作Objective-C选择器。 Otherwise, if your Swift class does not inherit from an Objective-C class, you need to prefix the symbol you want to use as a selector with the @objc attribute 否则,如果您的Swift类不继承自Objective-C类,则需要在要用作选择器的符号前添加@objc属性

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

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