繁体   English   中英

从视图层次结构中删除子视图并对其进行核对的正确方法是什么?

[英]What is the correct way to remove a subview from a view hierarchy and nuke it?

我有一个父UIView有很多子视图。 我需要定期删除子视图并将其从系统中完全删除。 这样做的正确方法是什么? 我试过这个:

UIView *v = [self.containerView viewWithTag:[n integerValue]];

[v removeFromSuperview];

并得到一个奇怪的结果。 以前出现的UIView消失了。 这是怎么回事?

尝试这个:

UIView *v = [self.containerView viewWithTag:[n integerValue]];
v.hidden = YES;
[self.containerView bringSubviewToFront:v];
[v removeFromSuperview];

我刚从UIView类文档中注意到的另一件事 - 请看最后一句:

removeFromSuperview取消接收器与其superview及其窗口的链接,并将其从响应器链中删除。

  • (空隙)removeFromSuperview

讨论如果接收者的超级视图不是零,则此方法释放接收者。 如果您计划重用该视图,请确保在调用此方法之前保留该视图,并确保在完成该视图或将其添加到另一个视图层次结构后适当地释放它。

在显示时不要调用此方法。

更新:现在是2014年,删除子视图而不隐藏它可以很好地完成。 原始海报的代码应按原样运作:

UIView *v = [self.containerView viewWithTag:[n integerValue]];
[v removeFromSuperview];

这将删除v及其作为子视图附加到它的任何视图,留下containerView和v的任何兄弟。

要从视图中删除所有子视图:

for(UIView *subview in [view subviews]) {
   [subview removeFromSuperview];
}

如果您只想删除某些特定视图:

for(UIView *subview in [view subviews]) {
  if([subview isKindOfClass:[UIButton class]]) {
     [subview removeFromSuperview];
 } else {
     // Do nothing - not a UIButton or subclass instance
 }
}

您还可以按标记值删除子视图:

for(UIView *subview in [view subviews]) {
    if(subview.tag==/*your subview tag value here*/) {
        [subview removeFromSuperview];

    } else {
        // Do nothing - not a UIButton or subclass instance
    }
}

Swift 3.0:

let viewToRemove = mySuperView.viewWithTag(myTag)
viewToRemove?.removeFromSuperview()

这是正确的总体思路。 那些消失的UIViews,他们与这个UIView的关系是什么? 他们是这个观点的子视图吗? 它们是否在您要移除的视图的dealloc方法中被解除了?

你确定你的标签是独一无二的吗?

Sujal

它们只是从显示屏上消失,还是从显示屏视图层次结构中消失了? 调试器向您展示了什么?

cell.contentView是否可能与要删除的子视图具有相同的标记? 根据文档 viewWithTag删除:

接收器层次结构中与标记匹配的视图。 接收器包含在搜索中。

如果是这种情况,那么您可能无意中从单元格中删除了cell.contentView。 如果n为零且您的单元格的contentview没有设置标记,则默认为0并导致该情况发生。

Swift 4:扩展UIView

extension UIView {
    public func removeAllSubviews() {
        for subview in self.subviews {
            subview.removeFromSuperview()
        }
    }
}

要么

extension UIView {
    public func removeAllSubviews() {
        self.subviews.forEach { $0.removeFromSuperview() }
    }
}
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)

        if let topController = UIApplication.topViewController() {

            if topController.isKind(of: ProviderHome.self)
            {
                let arrOfSuview = self.view.subviews

                if arrOfSuview.count > 1
                {
                    print("Davender Arr of subviews : \(arrOfSuview)")

                    for i in 0..<arrOfSuview.count
                    {
                        let objSub = arrOfSuview[i]

                        if objSub.tag == 101
                        {
                          objSub.removeFromSuperview()
                        }

                    }



                }


                NotificationCenter.default.addObserver(self, selector: #selector(ProviderHome.handelPushNotification), name: NSNotification.Name(rawValue: "handelPush"), object: nil)

                NotificationCenter.default.addObserver(self, selector: #selector(ProviderHome.handelLocalNotification), name: NSNotification.Name(rawValue: "handelLocal"), object: nil)
            }
        }


    }

@objc func handelPushNotification(_ notification: NSNotification)  {


        let arrOfSuview = self.view.subviews

        if arrOfSuview.count > 1
        {
            print("Davender Arr of subviews : \(arrOfSuview)")

            for i in 0..<arrOfSuview.count
            {
                let objSub = arrOfSuview[i]

                if objSub.tag == 101
                {
                    objSub.removeFromSuperview()
                }

            }

        }

        if notification.userInfo != nil
        {


            let dict = notification.userInfo as! Dictionary<String, Any>

            let d = dict["data"] as! Dictionary<String, Any>

            let action = d["gcm.notification.label"] as! String

            print("current message id :- ", action)

            self.getNotificationId = action

            if getNotificationId != ""
            {
               //call the api for getting Data

                AppDelegate.sharedInstance().myCurrentnotificationId = getNotificationId

                //working code
                let storyboard = UIStoryboard(name: "Provider", bundle: nil)
                let vc = storyboard.instantiateViewController(withIdentifier: "CommonPopUpsVC") as! CommonPopUpsVC
                vc.modalPresentationStyle = .overFullScreen
                vc.view.frame = self.view.frame
                vc.view.tag = 101
                self.view.addSubview(vc.view)
                self.present(vc, animated: true, completion: nil)

            }


       }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM