ما هي الطريقة الصحيحة لإزالة نموذج فرعي من طريقة عرض هرمية و Nuke ذلك؟

StackOverflow https://stackoverflow.com/questions/1516294

سؤال

لدي الوالد Uiview مع عدد من العروض الفرعية. بشكل دوري أحتاج إلى إزالة عرض فرعي وإزالته بالكامل من النظام. ما هي الطريقة الصحيحة للقيام بذلك؟ حاولت هذا:

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

[v removeFromSuperview];

وحصلت على نتيجة غريبة. سابقا تقديم UIViewاختفى s أيضا. ماذا يحدث هنا؟

هل كانت مفيدة؟

المحلول

جرب هذا:

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

شيء آخر لاحظته للتو من وثيقة فئة Uiview - راجع الجملة الأخيرة:

لا تنطبق RemoveFromSuperView إلغاء الاستقبال من Superview والنافذة الخاصة به، وتزيلها من سلسلة المستجيب.

  • (باطلة) removefromsuperview

مناقشة إذا لم يكن SuperView الخاص بالمستقبل، فإن هذه الطريقة تطلق جهاز الاستقبال. إذا كنت تخطط لإعادة استخدام الرأي، فتأكد من الاحتفاظ به قبل الاتصال بهذه الطريقة وتأكد من إصدارها حسب الاقتضاء عند الانتهاء منه أو بعد إضافته إلى التسلسل الهرمي للرؤية الأخرى.

لا تستدعي هذه الطريقة أبدا أثناء العرض.

تحديث: إنه الآن 2014 وإزالة نظرة فرعية دون إخفاءها يعمل بشكل جيد. يجب أن يعمل رمز الملصق الأصلي كما هو:

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

سيؤدي ذلك إلى إزالة هذا V وأي طرق عرض تعلق عليه كأجهزة فرعية، تاركا وراء حاوية الحاويات وأي أشقاء ضد 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؟ هل هم فرعيون من هذا الرأي؟ هل يتم deallec'd في طريقة Deallec من رأي أنك تقوم بإزالة؟

هل أنت متأكد من أن علاماتك فريدة من نوعها؟

ساجال

هل تختفي فقط من الشاشة، أو تختفي من الشاشة و عرض التسلسل الهرمي؟ ماذا يظهر لك مصحح الأخطاء؟

هل من الممكن أن تقوم Cell.contentview بنفس العلامة التي تريد إزالتها؟ بحسب ال توثيق Diewithtag يزيل:

الرأي في التسلسل الهرمي للمستقبل الذي يطابق العلامة. يتم تضمين جهاز الاستقبال في البحث.

إذا كان هذا هو الحال، فيمكنك إزالته عن غير قصد Cell.contentView من الخلية. إذا كان N هو الصفر، فلن يكون ل Cell 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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top