查看未从iOS中的超级视图中删除

时间:2018-01-31 10:15:47

标签: ios swift uiview

我有UIViewcollectionView。如果有互联网连接,我想隐藏collectionView并显示UIView,否则显示class MyClass{ @IBOutlet weak var collectionView: UICollectionView! var myView : CustomView? .... func internetStatusChanegd(){ if(isOnline){ collectionView.isHidden = true if let viewNib = UIView.loadFromNibNamed("CustomView", bundle: Bundle.main) as? CustomView { myView = viewNib myView!.frame = self.view.bounds self.view.addSubview(myView!) } }else{ if let customView = myView{ customView.removeFromSuperview() } collectionView.isHidden = false } } }

removeFromSuperview()

class CoursesViewController: UIViewController,UITextFieldDelegate {被调用,但视图未从视图中删除。你对这个问题有所了解吗?

4 个答案:

答案 0 :(得分:0)

像这样删除

--currencies

答案 1 :(得分:0)

  1. 在视图中添加子视图时,会为该视图添加标记。

  2. 在视图中迭代子视图的for循环。

  3. 在删除时,只需检查相同标签是否具有相同标签,然后调用 -

    self.removeFromSuperview()

答案 2 :(得分:0)

请在添加新视图之前删除已有的视图。

func internetStatusChanegd() {
    if(isOnline) {
        collectionView.isHidden = true
        for subView in (self.view.subviews)! {
            if (subView.tag == 100) {
                subView.removeFromSuperview() //this will remove already available object form self.view
            }
        }
        if let viewNib = UIView.loadFromNibNamed("CustomView", bundle: Bundle.main) as? CustomView {
            myView = viewNib
            myView!.frame = self.view.bounds
            myView.tag = 100 //add tag when you create object
            self.view.addSubview(myView!)
        }
    }else{
        if let customView = myView{
            customView.removeFromSuperview()
        }
        collectionView.isHidden = false}
    }
}

答案 3 :(得分:0)

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if isOnline{
        collectionView.backgroundView = myView //your custom view whatever you want to show here like : button..
        return 0
    }
    collectionView.backgroundView = nil
    return array.count
}
func internetStatusChanegd(){ collectionView.reloadData() } //it'll handle automatically that view .

试试这个