Swift 3da UIViewControllerni qanday taqqoslash mumkin?

Swift 3 da UIViewController bilan solishtirishga harakat qilaman, lekin ba'zi bir xato

extension UINavigationController
{
    func myPopToViewController(viewController:UIViewController, animated:Bool) -> UIViewController? {
        var arrViewControllers:[UIViewController] = []

        arrViewControllers = self.viewControllers
        for vc:UIViewController in arrViewControllers {
            if(vc.isKind(of: viewController) )//This Line gives me error
            {
                return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
            }

        }
        return nil
    }

}
/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:30: "UIViewController" turidagi qiymatini "AnyClass" (aka 'AnyObject.Type' )

va agar ishlatmoqchi bo'lsangiz

if(vc is viewController)

U beradi

/Users/varunnaharia/Documents/Projects/appname/appname/Public/UINavigationController+Extra.swift:18:22: Nomaqbul bo'lmagan "viewController" dan foydalanish

Buni men chaqiraman

self.navigationController?.popOrPopToViewController(viewController: MyUIViewController(), animated: false)
2
iltimos, qanday ishlashini tushuntiring. u xato qilmoqda ...
qo'shib qo'ydi muallif Varun Naharia, manba
if (vv UIViewController) ni ishlating
qo'shib qo'ydi muallif RajeshKumar R, manba
Agar vc - arrViewControllers {} bo'lsa, buni sinab ko'ring
qo'shib qo'ydi muallif Harshal Valanda, manba
Bu siz uchun ishlaydi if (vc.isKind (of: viewController.classForCoder)) {return (self.popToViewController (vc, jonlantirilgan: jonlantirilgan)). } .
qo'shib qo'ydi muallif SAURAV JUSTIN, manba
agar vc UIViewController {// vc sinf turi UIViewController bo'lsa}
qo'shib qo'ydi muallif Ashish Shah, manba

6 javoblar

Swift 4 Hope it will work for you

   extension UINavigationController {
  func myPopToViewController(viewController:UIViewController, animated:Bool) {
    var arrViewControllers:[UIViewController] = []
    arrViewControllers = self.viewControllers
    for vc:UIViewController in arrViewControllers {
      if(vc.isKind(of: viewController.classForCoder)){
         (self.popToViewController(vc, animated: animated))
      }
    }
  }

}
4
qo'shib qo'ydi

Tezda isKind (of:) o'rniga - foydalanamiz.

is is used to check the type of the object.

Shunday qilib,

if(vc is UIViewController)

Lekin men bu erda UIViewControllerning 2 ta havolasini topishga harakat qilyapman deb o'ylayman.

Shuning uchun - o'rniga === dan foydalanishingiz kerak. Ushbu operator bir xil turdagi 2 ta mos yozuvlarga mos kelish uchun ishlatiladi.

if(vc === viewController)
3
qo'shib qo'ydi
Izohingiz uchun tashakkur
qo'shib qo'ydi muallif Varun Naharia, manba
for viewsController in arrViewControllers
{
    if(viewsController.isKind(of: YourControllerClassName.self)){
    }
}
3
qo'shib qo'ydi
Tashakkur!
qo'shib qo'ydi muallif Ammar Mujeeb, manba
"UIViewController" tipidagi qiymatni kutilgan o'zgaruvchiga aylantira olmaysiz: Users/varunnaharia/Documents/Projects/appname/appname/Public & zwnj;/UINavigationControl & zwnj; ler + Extra.swift: 18: 3 & zwnj; 'AnyClass' (aka 'AnyObject.Type')
qo'shib qo'ydi muallif Varun Naharia, manba
Siz bergan kodni ishlatganmisiz?
qo'shib qo'ydi muallif Swift_Guru, manba
Qaerda xato qilasiz?
qo'shib qo'ydi muallif Swift_Guru, manba

Agar muayyan ko'rinish tekshirgich bilan solishtirishni istasangiz, ularning referentlarini solishtirishingiz kerak.

Buni ko'ring ...

if(vc === viewController) )
{
    return (self.navigationController?.popToViewController(vc, animated: animated)?.last)!
}
1
qo'shib qo'ydi

Ehtimol, siz nimani qidirmoqdasiz, ya'ni, bu boshqaruvchi hozirgi navigatsiya to'plamidagi ob'ekt bo'lgan joyga berilgan boshqaruvchiga qadar chiqadigan usuldir.

Shunday qilib,

Mana, bir tekshiruvchi ustidagi barcha tekshirgichlarni ochadigan kengaytma usuli.

extension UINavigationController {
    func popTo(controllerToPop:UIViewController)  {
        let controllers = self.viewControllers
        for controller in controllers {
            if(controller == controllerToPop) {
                self.popTo(controllerToPop: controllerToPop)
            }
        }
    }
}
0
qo'shib qo'ydi

Faqatgina janob @BangOperatorning muayyan View controllerga o'tish uchun javobini o'zgartiraman.

extension UINavigationController {

func popTo(controllerToPop:UIViewController)  {

    //1. get all View Controllers from Navigation Controller
    let controllersArray = self.viewControllers

    //2. check whether that view controller is exist in the Navigation Controller
    let objContain: Bool = controllersArray.contains(where: { $0 == controllerToPop })

    //3. if true then move to that particular controller
    if objContain {
        self.popToViewController(controllerToPop, animated: true)
    }
  }
}
0
qo'shib qo'ydi