试图从另一个类中删除子视图

时间:2019-05-05 08:33:23

标签: ios swift

我具有以下用于在视图顶部创建覆盖/子视图的功能:

func testOverLay(shouldShow: Bool){

    let iconImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
        iv.clipsToBounds = true
        return iv

    }()
    let blueCover = UIView(frame: iconImageView.frame)
    blueCover.backgroundColor = UIColor.black
    blueCover.layer.opacity = 0.3
    blueCover.isUserInteractionEnabled = false
    blueCover.tag = 100

    let navshadowImage: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.frame = CGRect(x: 0, y: 0, width: self.navigationController!.navigationBar.frame.size.width, height: self.navigationController!.navigationBar.frame.size.height + statusBarHeight!)
        iv.clipsToBounds = true
        return iv

    }()
    let navBlackCover = UIView(frame: navshadowImage.frame)
    navBlackCover.backgroundColor = UIColor.black
    navBlackCover.layer.opacity = 0.3
    navBlackCover.isUserInteractionEnabled = false
    navBlackCover.tag = 200

    if shouldShow{
        view.addSubview(blueCover)
        self.navigationController?.view.addSubview(navBlackCover)

    }else{
        removeSubview(firstTag: 100, secondTag: 200)
    }
}

我通过调用removeSubview来删除它:

func removeSubview(firstTag: Int, secondTag: Int){
    print("removeSubview was called")
    if let viewWithTag = self.view.viewWithTag(firstTag) {
        viewWithTag.removeFromSuperview()
    }
    if let viewWithTag = self.navigationController?.view.viewWithTag(secondTag){
        viewWithTag.removeFromSuperview()

    }
}

当我从同一个类中调用它时,这可以很好地工作。


但是我想从另一个班级来称呼它。我创建了一个可以调用该函数的委托,然后从第二个类中调用它,如下所示:

model.removeSubview(firstTag: 100, secondTag: 200)

但是由于某些原因,视图不会被删除。

该函数确实从我的“第二类”中被调用,我可以在控制台中看到打印语句。

我不确定在创建其他类时标签是否会被删除?

任何帮助将不胜感激。

0 个答案:

没有答案