按钮点击不适用于自定义UIView

时间:2018-10-15 10:02:05

标签: ios swift xcode

自定义UIView的代码:

也请在此处查看视频:https://drive.google.com/open?id=1kbrOxXWcJIi4vkiqMNer3exBr5cOWgDz

 import UIKit
    protocol PostAttachmentFullScreenViewDelegate: class {
        func closeAttachmentFullView()
    }

    class PostAttachmentFullScreenView: UIView {

        weak var delegate: PostAttachmentFullScreenViewDelegate?

        @IBOutlet var backgroundView: UIImageView!

        @IBOutlet var closeButton: UIButton!
        @IBAction func closeViewAction(_ sender: Any) {
            print("will call delegate to put it off")
            self.delegate?.closeAttachmentFullView()
        }


        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)

            let _ = commonInitialization()
            backgroundView.image = UIImage(named: "ScrollImageTop1")
           closeButton.isUserInteractionEnabled = true
        }

        override init(frame: CGRect) {
            super.init(frame: frame)
             let _ = commonInitialization()

            backgroundView.image = UIImage(named: "ScrollImageTop1")
            closeButton.isUserInteractionEnabled = true

        }

        func commonInitialization() -> UIView
        {
            let bundle = Bundle.init(for: type(of: self))
            let nib = UINib(nibName: "PostAttachmentFullScreenView", bundle: bundle)
            let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
            view.frame = bounds
            view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
            addSubview(view)
            return view

        }


    }

在ViewController中的用法(我正在定义自定义视图的实例并将其放入“滚动视图”中):

        var frame  = CGRect(x:0, y:0, width:0, height:0)
        let blue = PostAttachmentFullScreenView()
        blue.delegate = self
        blue.isUserInteractionEnabled = true
        blue.backgroundColor = UIColor.blue
        blue.backgroundView.image = fileAttachments[1]

        frame.origin.x = attachmentsScrollView.frame.size.width * CGFloat (0)
        frame.size = attachmentsScrollView.frame.size
        blue.frame = frame
        attachmentsScrollView.addSubview(blue)


extension NewPostViewController : PostAttachmentFullScreenViewDelegate
{
    func closeAttachmentFullView() {
        print("hiding attachments view")
        attachmentSuperView.isHidden = true
    }
}

令我惊讶的是,它甚至没有打印-“将呼叫委托将其推迟”。 我无法理解这里出了什么问题。请帮助我了解问题并解决。谢谢。

3 个答案:

答案 0 :(得分:1)

您正在混合使用编程方法和xib方法。

您添加了IBOultetIBAction,这意味着您将xib用于UIView

在那种情况下,您必须在初始化视图时加载UIView xib

在项目中为extension添加一个UIView

extension UIView {
    class func fromNib<T: UIView>() -> T {
        return Bundle.main.loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
    }
}

初始化视图时,请像这样添加它:

let blue : PostAttachmentFullScreenView = UIView.fromNib()
blue.delegate = self
blue.isUserInteractionEnabled = true
blue.backgroundColor = UIColor.blue
blue.backgroundView.image = fileAttachments[1]

frame.origin.x = attachmentsScrollView.frame.size.width * CGFloat (0)
frame.size = attachmentsScrollView.frame.size
blue.frame = frame
attachmentsScrollView.addSubview(blue)

,委托和按钮操作方法将起作用。

您错过了:

enter image description here

答案 1 :(得分:0)

您永远不会在按钮上设置目标/操作。您需要在某个地方致电addTarget(_:action:for:)来设置按钮上的目标/操作。还有,什么将按钮作为插座连接到您的PostAttachmentFullScreenView

答案 2 :(得分:0)

这可能是一个显而易见的问题,但对我来说(Xcode 10.1)将所有缺少的UI约束添加到有问题的UIButton中(至少4个约束),在我的自定义视图中为我修复了错误:

Add New Constrain button at the bottom

Add New Constraint options

请确保您添加了足够的约束(通常是4个约束)或足以删除所有有关缺少约束的警告。执行完此操作并用Ctrl键附加按钮后,再从View拖动到相应的快速代码,即可检测到单击并正常工作。

希望这会有所帮助。