【问题标题】:Swift4 ViewController not fired event in subViewSwift4 ViewController 未在 subView 中触发事件
【发布时间】:2018-04-20 20:11:17
【问题描述】:

我有 1 个来自故事板的 MainViewController 和 1 个来自 xib 的 ModalUIView。

在 ModalUIView 中具有显示模态的呈现功能和关闭模态的关闭功能。

步骤: MainViewController -> OpenModal -> ModalUIView -> CloseModal

这是我的代码:

UIViewUtil.swift

import Foundation
import UIKit

extension UIView {
    // Load xib as the same name of CustomView that want to use xib
    func loadXib() -> UIView{
        let bundle = Bundle(for: type(of: self))
        let nibName = type(of: self).description().components(separatedBy: ".").last!
        let nib = UINib(nibName: nibName, bundle: bundle)
        return nib.instantiate(withOwner: self, options: nil).first as! UIView
    }
}

MainViewController.swift 是 UIViewController 的子类

@IBAction func guideButton(_ sender: Any) {
    let modal = ModalUIView()
    modal.present(targetView: self.view)
}

ModalUIView.swift 是 UIView 的子类

var view : UIView?

override init(frame: CGRect) {
    super.init(frame: frame)

    setup()
}

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

    setup()
}

func setup() {
    view = loadXib()
}

func present(targetView: UIView) {
    view!.layer.cornerRadius = 10.0
    view!.clipsToBounds = true

    targetView.addSubview(view!)

    // Set size
    let popupWidth: CGFloat = targetView.frame.width - (targetView.frame.width * 0.04)
    let popupHeight: CGFloat = targetView.frame.height - (targetView.frame.height * 0.08)

    view!.frame = CGRect(x: targetView.frame.origin.x, y: targetView.frame.origin.y,
                         width: popupWidth, height: popupHeight)

    view!.center = targetView.center

    view!.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
    view!.alpha = 0

    UIView.animate(withDuration: 0.4){
        self.view!.alpha = 1
        self.view!.transform = CGAffineTransform.identity
    }
}

@objc func dismiss(sender: UIButton!) {
    print("dismiss")
}

当我调用 modalUIView 的存在时,我的问题是 mainViewController,然后我在 modalUIView 中的 closeButton 选项卡上没有触发该操作

我尝试使用@IBAction,但它不起作用:

@IBAction func CloseButtonAction(_ sender: UIButton) {
}

我也尝试通过编程方式手动添加操作,但它也不起作用:

let closeButton: UIButton? = view?.viewWithTag(10) as! UIButton
closeButton!.addTarget(self, action: #selector(dismiss), for: .touchUpInside)

注意: 我可以看到并点击模式上的 closeButton。 我已经将 ModalUIView 添加到 xib 文件的所有者

【问题讨论】:

    标签: ios swift uiview uiviewcontroller subview


    【解决方案1】:

    你也可以在你的视图控制器中获得一个动作。

    yourSubView.button.addTarget(self, action: #selector(buttonPress(sender:)), for: .touchUpInside)
    

    它会调用你的方法。

    func buttonPress(sender:UIButton){
    print("Button pressed")
    }
    

    【讨论】:

      【解决方案2】:

      好的,我现在有一个解决方案,但不确定这是最佳答案。

      我的解决方案是通过不在modalUIView中的mainViewController向modalUIView的closeButton添加一个动作

      如果有人有比这更好的解决方案,请给我建议。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-15
        相关资源
        最近更新 更多