首页 > 解决方案 > 从另一个类访问图像

问题描述

我有一个快速的问题。我正在尝试从另一个视图控制器设置图像。我究竟做错了什么?

主视图控制器:

//我如何设置弹出视图:

lazy var popUpView: PopViews = {
        let view = PopViews()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.layer.cornerRadius = 24
        view.delegate = self
        return view
    }()

弹出视图的调用

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()){
            self.handleShowPop()
        }

handleShowPop 函数:

@objc func handleShowPop(){
        view.addSubview(popUpView)

        let image: UIImage = UIImage(named: "Test")!
    popUpView.imageView = UIImageView(image: image)


        popUpView.button.setTitle("View Our Menu2", for: .normal)

        popUpView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -40).isActive = true
        popUpView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        popUpView.heightAnchor.constraint(equalToConstant: view.frame.width - 154).isActive = true
        popUpView.widthAnchor.constraint(equalToConstant: view.frame.width - 104).isActive = true

        popUpView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        popUpView.alpha = 0

        UIView.animate(withDuration: 0.5){
            self.visualEffectView.alpha = 0.7
            self.popUpView.alpha = 1
            self.popUpView.transform = CGAffineTransform.identity
        }
    }

这是 PopViews 类:

我可以在此处添加图像,但无法从第一个视图控制器访问它

 class PopViews: UIView {

     var imageView: UIImageView = {


let img = UIImageView(image: #imageLiteral(resourceName: "test"))
            img.translatesAutoresizingMaskIntoConstraints = false
            img.heightAnchor.constraint(equalToConstant: 80).isActive = true
            img.widthAnchor.constraint(equalToConstant: 80).isActive = true
            img.image = imgz

            return img
        }()
    }

标签: iosswiftxcode

解决方案


推荐阅读