UIWindow yordamida viewViewControllerni oling?

Shunday qilib, self kodi UIWindow deb nomlanadi, ammo qanday qilib hozirda ko'rinadiganViewControllerni olishim mumkin?

0
UINavigationControllerdan foydalanasizmi?
qo'shib qo'ydi muallif Hardik Shekhat, manba

10 javoblar

Agar siz derazadagi eng yuqori ko'rinishni xohlasangiz, bu ko'rinishga ega bo'lasiz.

[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
1
qo'shib qo'ydi

Agar siz derazadagi eng yuqori ko'rinishni xohlasangiz, bu ko'rinishga ega bo'lasiz.

[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
1
qo'shib qo'ydi

Hiyerarşide eng yuqori ko'rish denetleyicisine murojaat qilish uchun, quyidagi kodni harakat qilib ko'ring

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController)
{
    topController = topController.presentedViewController;
}

return topController;
1
qo'shib qo'ydi

Hiyerarşide eng yuqori ko'rish denetleyicisine murojaat qilish uchun, quyidagi kodni harakat qilib ko'ring

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController)
{
    topController = topController.presentedViewController;
}

return topController;
1
qo'shib qo'ydi

Tezda 3:

func getVisibleViewController(_ rootViewController: UIViewController?) -> UIViewController? {

    var rootVC = rootViewController
    if rootVC == nil {
        rootVC = UIApplication.shared.keyWindow?.rootViewController
    }

    if rootVC?.presentedViewController == nil {
        return rootVC
    }

    if let presented = rootVC?.presentedViewController {
        if presented.isKind(of: UINavigationController.self) {
            let navigationController = presented as! UINavigationController
            return navigationController.viewControllers.last!
        }

        if presented.isKind(of: UITabBarController.self) {
            let tabBarController = presented as! UITabBarController
            return tabBarController.selectedViewController!
        }

        return getVisibleViewController(presented)
    }
    return nil
}
1
qo'shib qo'ydi

Tezda 3:

func getVisibleViewController(_ rootViewController: UIViewController?) -> UIViewController? {

    var rootVC = rootViewController
    if rootVC == nil {
        rootVC = UIApplication.shared.keyWindow?.rootViewController
    }

    if rootVC?.presentedViewController == nil {
        return rootVC
    }

    if let presented = rootVC?.presentedViewController {
        if presented.isKind(of: UINavigationController.self) {
            let navigationController = presented as! UINavigationController
            return navigationController.viewControllers.last!
        }

        if presented.isKind(of: UITabBarController.self) {
            let tabBarController = presented as! UITabBarController
            return tabBarController.selectedViewController!
        }

        return getVisibleViewController(presented)
    }
    return nil
}
1
qo'shib qo'ydi

Agar bolani ko'rish tekshirgichini qo'shsangiz:

let viewControllersVisible = self.rootViewController?.childViewControllers.filter({ $0.isVisible && $0.view.window })

Bu ko'rinishdagi ierarxiyangizga qo'shilgan UIViewControllers qatorini qaytaradi, foydalanuvchi aslida ushbu ko'rinishlarni boshqarish mumkinligini bilmaydi, sizning ierarxikingizga bog'liq.

Agar moddiy jihatdan faqat bitta ko'rish tekshiruvchisini taklif qilsangiz:

let viewControllerVisible = self.rootViewController?.presentedViewController
0
qo'shib qo'ydi

Agar bolani ko'rish tekshirgichini qo'shsangiz:

let viewControllersVisible = self.rootViewController?.childViewControllers.filter({ $0.isVisible && $0.view.window })

Bu ko'rinishdagi ierarxiyangizga qo'shilgan UIViewControllers qatorini qaytaradi, foydalanuvchi aslida ushbu ko'rinishlarni boshqarish mumkinligini bilmaydi, sizning ierarxikingizga bog'liq.

Agar moddiy jihatdan faqat bitta ko'rish tekshiruvchisini taklif qilsangiz:

let viewControllerVisible = self.rootViewController?.presentedViewController
0
qo'shib qo'ydi

Siz ushbu javobni tekshirishingiz kerak. Buning xulosasi sizning oyna .rootViewController dan boshlang. O'z kodamda ( .rootViewController UINavigationController yordamida) buni ( AppDelegate ) dan foydalaning:

if let nvc = self.window?.rootViewController as? UINavigationController {
    if let mvc = nvc.topViewController as? MasterViewController {
       //... do something
    } else if let dvc = nvc.topViewController as? DetailViewController {
       //... do something
    }
}

Agar Master-Detail ilovasi uchun standart shablonni ishlatayotgan bo'lsangiz, uni o'zingiz ichiga olgan SplitViewController ni ko'rib chiqishingiz kerak, lekin bu kodlash kodidan juda aniq bo'lishi kerak.

0
qo'shib qo'ydi

Siz ushbu javobni tekshirishingiz kerak. Buning xulosasi sizning oyna .rootViewController dan boshlang. O'z kodamda ( .rootViewController UINavigationController yordamida) buni ( AppDelegate ) dan foydalaning:

if let nvc = self.window?.rootViewController as? UINavigationController {
    if let mvc = nvc.topViewController as? MasterViewController {
       //... do something
    } else if let dvc = nvc.topViewController as? DetailViewController {
       //... do something
    }
}

Agar Master-Detail ilovasi uchun standart shablonni ishlatayotgan bo'lsangiz, uni o'zingiz ichiga olgan SplitViewController ni ko'rib chiqishingiz kerak, lekin bu kodlash kodidan juda aniq bo'lishi kerak.

0
qo'shib qo'ydi