Kuzatuv tekshiruvi tekshiruvi qanday qilib to'plamga qo'shiladi yoki qilinmaydi

Ikkita ko'rinish nazoratchi bor. Quyidagi kodni ishlatish uchun tugmani bosib, men bir ko'rinishdan boshqa ko'rinishga harakat qildim.

 *let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("NotificationController") as! NotificationController

self.navigationController!.pushViewController(secondViewController, animated: true)*

Orqa tomonda, bar kodidan foydalanib, orqaga qaytish uchun bar tugmachasini bosing.

 self.navigationController?.popViewControllerAnimated(true)

Shu sababli mening muammom - agar men bir nuqtai nazardan doimiy ravishda boshqa fikrga boradigan bo'lsam, unda u suyakka qo'shiladi. Uni faqat to'xtatib qo'yish uchun stakka qo'shilganda boshqa ko'rinishni ko'rsatishni istayman. Bu faqat bir marta qo'shiladi.

5
stackoverflow.com/a/42523549/5461400 bu harakat qilib ko'ring
qo'shib qo'ydi muallif Harshal Valanda, manba

6 javoblar

navigatsiya to'plamining view controller turini o'z ichiga olganligini tekshirish uchun siz quyidagilarni foydalanishingiz mumkin:

if let viewControllers = self.navigationController?.viewControllers
{
    if viewControllers.contains(where: {
        return $0 is YourViewController
    })
    {
        //Write your code here
    }
}

Muayyan tekshirgichni navigatsiya stackidan olib tashlash uchun navigatsiya to'plamini o'zgartirishingiz kerak.

Misol:

    if var viewControllers = self.navigationController?.viewControllers
    {
        for controller in viewControllers
        {
            if controller is UIViewController
            {
                viewControllers.removeElement(controller)
                self.navigationController?.viewControllers = viewControllers
            }
        }
    }
10
qo'shib qo'ydi
Array ustida 'removeElement ()' usuli yo'q. Lekin viewControllers xususiyatini filtrlash bilan bir xil narsalarni amalga oshirishingiz mumkin. Iltimos, javob uchun javobingizni va javobingizni ishlab chiqishni ko'rsating. Cheers.
qo'shib qo'ydi muallif Womble, manba

Tez 4 uchun

 if let viewControllers = self.navigationController?.viewControllers {
       for vc in viewControllers {
            if vc.isKind(of: YourViewController.classForCoder()) ){
                 print("It is in stack")
                 //Your Process
            }
       }
 }
5
qo'shib qo'ydi

Buni tekshirish uchun kod.

if let viewControllers = navigationController?.viewControllers {
    for viewController in viewControllers {
       //some process
        if viewController.isKindOfClass(ViewControllerClassName) {
            println("yes it is")
        }
    } 
}
2
qo'shib qo'ydi
vc.removeFromParentViewController- Ushbu usuldan foydalaning
qo'shib qo'ydi muallif Sivajee Battina, manba
Bundan tashqari, ushbu self.navigationController-ni ishlatishingiz mumkin.viewControllers.removeAtIndex (vcI & zwnj; ndex), indeks raqamini bilsangiz
qo'shib qo'ydi muallif Sivajee Battina, manba
Buni foydali deb topdingizmi?
qo'shib qo'ydi muallif Sivajee Battina, manba
Qachon qo'shish ekstre qilib olib tashlash
qo'shib qo'ydi muallif Gaurav Gupta, manba
Men to'plamdan olib tashlanganda yana. Ushbu kodni ishlatib, bu to'plamga qo'shilmagan.
qo'shib qo'ydi muallif Gaurav Gupta, manba

PGDevning javobiga ko'ra, Swift 4.1 uchun

UINavigationController to'plamidan muayyan UIViewController subclass qanday chiqariladi:

/// Given 'nc' is a valid UINavigationController instance,
/// removes all instances of MyViewController from the stack

nc.viewControllers = nc.viewControllers.filter { !($0 is MyViewController) }

Kengaytma qo'yish:

extension UINavigationController
{
    /// Given the kind of a (UIViewController subclass),
    /// removes any matching instances from self's
    /// viewControllers array.

    func removeAnyViewControllers(ofKind kind: AnyClass)
    {
        self.viewControllers = self.viewControllers.filter { !$0.isKind(of: kind)}
    }

    /// Given the kind of a (UIViewController subclass),
    /// returns true if self's viewControllers array contains at
    /// least one matching instance.

    func containsViewController(ofKind kind: AnyClass) -> Bool
    {
        return self.viewControllers.contains(where: { $0.isKind(of: kind) })
    }
}

Foydalanuvchi:

guard let nc = self.navigationController else { return }

let exists = nc.containsViewController(ofKind: MyViewController.self)

nc.removeAnyViewControllers(ofKind: MyViewController.self)

BTW, agar "UIViewController" ning quyi sinflariga qanday qilib "tur" ni qanday qilib cheklashni biladigan bo'lsa, iltimos, tashqariga chiqmasin.

1
qo'shib qo'ydi
func o'z ichiga oladiViewController (ofKind turdagi: UIViewController.Type) -> Bool {return self.viewControllers.contains (bu erda: {$ 0.isKind (of: kind)})}}
qo'shib qo'ydi muallif appleitung, manba

Qani boshladik.

Ushbu qator sizga UIViewControllers qatorini beradi

self.navigationController? .viewControllers

Endi nima qilish kerakligini ko'rishingizControllerObject mavjudmi yoki yo'qmi?

Ushbu qatorni yozish orqali

if viewController.isKindOfClass(YourController){
}

va bu erda to'liq kod.

if let viewControllers = self.navigationController?.viewControllers {
            for viewController in viewControllers {

                if viewController.isKindOfClass(YourController) {
                    print("Your controller exist")
                }
       } 
}

"ViewControllerA" ga qaytib ketayotib, quyidagi satrni yozganingizda, ViewControllerB navigatsiya to'plamidan olib tashlanadi.

self.navigationController?.popViewControllerAnimated(true)

Bu biz kabi o'xshash pop operatsiyalari va biz navigatsiya nazoratchisi bilan ishlaymiz.

Agar biron-bir narsalaringiz bo'lsa, menga xabar bering.

1
qo'shib qo'ydi

You can check with below code

Maqsadi - C.

NSArray * controllers = [self.navigationController viewControllers];

    for (int i = 0; i < [controllers count]; i++){

        UIViewController * controllerTest = [controllers objectAtIndex:i];

        if([controllerTest isKindOfClass:[YourController class]]){
            NSLog(@"Class is available");
        }

    }

Swift 3.0

if let viewControllers = self.navigationController?.viewControllers {
            for viewController in viewControllers {
               //some process
                if viewController.isKindOfClass(YourController) {
                    print("Class is available")
                }
            } 
        }
0
qo'shib qo'ydi
Sinf mavjud bo'lganda yoki faqat suyakka qo'shilmasdan ko'rsatiladi
qo'shib qo'ydi muallif Gaurav Gupta, manba