Swift 从另一个 class 中删除子视图

Swift remove subview from another class

我有我的 ViewController 'chatPage',我在其中使用名为 'chatOverlay' 的 xib 文件呈现 UIView。我在 xib 中放置了一个按钮,按下时我想从 chatPage ViewController.

中删除子视图

我不确定该怎么做,因为该按钮与子视图位于不同的 class 中,并且没有引用我希望从中删除子视图的 ViewController。

我知道我可以使用通知观察器来通知 viewController 但是有其他方法吗?

当点击按钮时,如何从 chatOverlay class 中删除子视图?

chatOverlay:

import Foundation

class chatOverlay: UIView {
    @IBAction func closeOverlay(_ sender: UIButton) {
        print("CLOSE OVERLAY")
    }
}

聊天页面:

class chatPage: UIViewController {
    override func viewDidLoad(){
         view.addSubview(overlayAdd().show(height: view.frame.size.height, width: view.frame.size.width, x: view.frame.origin.x, y: view.frame.origin.y, tag: 101))
    }
}

覆盖添加:

import Foundation
import UIKit

class overlayAdd {

   func show(height: CGFloat, width: CGFloat, x: CGFloat, y: CGFloat, tag: Int) -> UIView{
         let chatOverlay = xibLoad().chatOverlay()                  
         chatOverlay.frame.size.height = height
         chatOverlay.frame.size.width = width
         chatOverlay.frame.origin.x = x
         chatOverlay.frame.origin.y = y
         chatOverlay.tag = tag

         let view = chatOverlay

         return view
   }

   func remove(tag: Int){ }
}

您可以使用self.removeFromSuperview()

// Completely agree with comments, change to ChatOverlay
class chatOverlay: UIView {  
    @IBAction func closeOverlay(_ sender: UIButton) {
        self.removeFromSuperview()
    }
}

如果你想让它消失

// Again, completely agree with comments, change to ChatOverlay
class chatOverlay: UIView {
    @IBAction func closeOverlay(_ sender: UIButton) {
        UIView.animate(withDuration: 0.5, animations: {
            self.alpha = 0.0
        }) { (_) in
            self.removeFromSuperview()
        }
    }
}

我真的很好奇你是如何在 chatOverlay 中使用 UIView 的,而你似乎只是在导入 Foundation?另外,无法弄清楚为什么在 overlayAdd 中要同时导入 FoundationUIKitUIKit 本身导入 Foundation。对我来说最大的困惑是为什么你有一个 class overlayAddshow 函数不应该更像是一个 create 函数(在将它添加为子视图之前不会显示它)并且只是 chatOverlay 的一部分吗?类似于:

// Completely agree with comments, change to ChatOverlay 
class chatOverlay: UIView {
    @IBAction func closeOverlay(_ sender: UIButton) {
    UIView.animate(withDuration: 0.5, animations: {
            self.alpha = 0.0
        }) { (_) in
            self.removeFromSuperview()
        }
    }

    static func create(withFrame: frame, tag: Int) -> UIView{
         let chatOverlay = xibLoad().chatOverlay()                  
         chatOverlay.frame = frame
         chatOverlay.tag = tag
         /*
         What in the world are you trying to do here???

         let view = chatOverlay

         return view

         get rid of these two completely useless lines!
         just return chatOverlay like below...
         */
         return chatOverlay
   }
}

然后在聊天页面:

// Completely agree with comments, change to ChatPage
class chatPage: UIViewController {
    override func viewDidLoad(){
         view.addSubview(chatOverlay.create(withFrame: view.frame, tag: 101))
    }
}