簡體   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