【问题标题】:UIButton inside UIView extensionUIView 扩展中的 UIButton
【发布时间】:2015-02-07 20:33:23
【问题描述】:

我想知道如何扩展 UIView 并在扩展函数中处理 UIButton 的目标选择器,但我没有做对。它根本不响应目标选择器。我什至尝试过使用代理披露。这是代码。

扩展 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;
        }
    }


}

}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    尝试在执行方法前添加@objc,即@objc func execute()...

    this link

    如果你的 Swift 类继承自一个 Objective-C 类,那么所有的 类中的方法和属性可用作 Objective-C 选择器。否则,如果您的 Swift 类不继承自 Objective-C 类,你需要在你想要使用的符号前面加上前缀 带有@objc 属性的选择器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 2017-02-07
      • 2016-02-10
      相关资源
      最近更新 更多