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

时间:2018-07-13 17:06:37

标签: swift subview

我有ViewController'chatPage',其中使用名为'chatOverlay'的xib文件提供了UIView。我在xib内放置了一个按钮,当按下该按钮时,我想从chatPage ViewController中删除该子视图。

我不确定如何执行此操作,因为按钮与子视图不在同一个类中,并且没有引用我希望从中删除子视图的ViewController。

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

点击按钮后,如何从chatOverlay类中删除子视图?

chatOverlay:

import Foundation

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

chatPage:

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))
    }
}

overlayAdd:

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){ }
}

1 个答案:

答案 0 :(得分:0)

您可以使用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()
        }
    }
}

我真的很好奇,当您似乎只导入UIView时,如何在chatOverlay中使用Foundation?另外,无法确定为什么在overlayAdd中同时导入FoundationUIKitUIKit本身会导入Foundation。对我最大的困惑是,为什么您根本没有课程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
   }
}

,然后在chatPage中:

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