来自UIStoryboardSegueTemplate的Destination ViewController

问题描述 投票:0回答:2

我试图以编程方式使用UIStoryboardSegueTemplate将所有segue附加到UIViewController,我的目标是将目标视图控制器连接到segue。

到目前为止我做了:

func initViewControllers(){

    guard let array = self.value(forKey: "storyboardSegueTemplates") as? [AnyObject] else {
        //No segues
        return
    }

    for item in array{
        print("\(logClassName): initViewControllers -> Self = \(String(describing: item.self))")
        print("\(logClassName): initViewControllers -> Segue Identifier = \(String(describing: item.value(forKey: "identifier")))")
        print("\(logClassName): initViewControllers -> Class = \(String(describing: item.value(forKey: "segueClassName")))")
        print("\(logClassName): initViewControllers -> Destination = \(String(describing: item.value(forKey: "destinationViewControllerIdentifier")))")


        let segueClassName:String = (item.value(forKey: "segueClassName") as? String) ?? ""
        if segueClassName.contains("CustomTabSegue"){
            print("\(logClassName): appending tabItem VC")
            let segueContainer:String = (item.value(forKey: "identifier") as? String) ?? ""
            performSegue(withIdentifier: segueContainer, sender: self)
        }

   }
}

并使用override func prepare(for segue: UIStoryboardSegue, sender: Any?)获取目的地

我选择我想要捕获的segues的方式是一个名为CustomTabSegue的自定义类

问题是:有没有办法在不执行segue的情况下获取目标View控制器?

根据这个link似乎不太可能,但也许有一些我想念的东西。

ios swift
2个回答
1
投票

如果您自己创建了这些segues,我做了什么,我将标识符给了segue: -

ViewControllerName1 + "To" + ViewControllerName2

在从storyboardSegueTemplates获取segue列表后,我将它们与To分开,得到了源和目标ViewControllers的名称。然后,将它们转换为Class类型

NSClassFromString(ClassName: String)

这样你就可以获得与segue相关的类的类型,但是不可能获得destinationViewControllers的实例,因为它们在你执行以下操作之前不会被初始化:

 performSegue(withIdentifier: String, sender: Any?)

希望能帮助到你!!


0
投票

根据史密斯特工的回答,我能够做到:

func initViewControllers(){

    guard let array = self.value(forKey: "storyboardSegueTemplates") as? [AnyObject] else {
        return
    }

    for item in array{

        if let segueClassName = (item.value(forKey: "segueClassName") as? String){

            if segueClassName.contains("CustomTabSegue"){
                print("\(logClassName): CustomTabSegue")

                if let destinationViewController = (item.value(forKey: "destinationViewControllerIdentifier") as? String)?.components(separatedBy: "-").first{
                    print("\(logClassName): DestinationVC = \(destinationViewController)")

                    if let addedVC = destinationViewController.getViewController(){
                        print("\(logClassName): Adding View Controller \(addedVC.logClassName)")
                        viewControllers.append(addedVC)
                    }

                }
            }
        }
}

我创建了以下扩展名

import Foundation
import UIKit

extension String{

func getViewController() -> UIViewController? {

    if var appName = Bundle.main.infoDictionary?["CFBundleName"] as? String {
        print("CFBundleName - \(appName)")
        appName = appName.replacingOccurrences(of: " ", with: "_", options: .literal)

        if let viewControllerType = NSClassFromString("\(appName).\(self)") as? UIViewController.Type {
            print("String: as UIViewController")
            return viewControllerType.init()
        }

    }

    return nil
}

它按预期工作,但现在唯一的问题是如果嵌入在UINavigationController中的ViewController它不会检测到它

© www.soinside.com 2019 - 2024. All rights reserved.