Swift: programmatically enumerate outgoing segues from a UIVIewController

后端 未结 2 1580
-上瘾入骨i
-上瘾入骨i 2020-12-19 16:17

I want to list the outgoing segues from a UIViewController, as described in Programmatically enumerate outgoing Segues for a UIViewController, but in Swift. (Swift 2, Xcode

相关标签:
2条回答
  • 2020-12-19 16:54

    this valueForKey("storyboardSegueTemplates") is UNDOCUMENTED property and UIStoryboardPresentationSegueTemplate is UNDOCUMENTED class. Beware of rejection from App Store if you are uploading application to App Store.

    If you want to use this in your in-house projects, use as following

    for template in (valueForKey("storyboardSegueTemplates") as? [AnyObject])! {
        if let identifier = template.valueForKey("identifier") as? String {
            print("identifier - " + identifier)
        }
        else {
            print("no identifier for \(template)")
        }
    }
    

    Found from https://github.com/JaviSoto/iOS9-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIStoryboardSegueTemplate.h

    0 讨论(0)
  • 2020-12-19 17:09

    As per Swift 4.2 and from https://stackoverflow.com/a/35060917/1058199. Thanks /johnykutty.

    import UIKit
    
    extension UIViewController {
    
         // Segue aids in Swift
         @objc func isValidSegue(_ segueId: String?) -> Bool {
            let filteredArray = (value(forKey: "storyboardSegueTemplates") as? NSArray)?.filtered(using: NSPredicate(format: "identifier = %@", segueId ?? ""))
            let isValid = (filteredArray?.count ?? 0) > 0
            return isValid
         }
    
         @objc func segues() -> Array<Any>? {
            let segues = self.value(forKey: "storyboardSegueTemplates")
            return segues as! Array<Any>?
         }
    
        @objc func segueNames() -> Array<AnyHashable> {
            var segueNames = Array<Any>()
    
            let filteredArray = (value(forKey: "storyboardSegueTemplates") as? NSArray)?.filtered(using: NSPredicate(format: "identifier != nil" ))
    
            for template in filteredArray! as [AnyObject] {
               if let identifier = (template.value(forKey: "identifier") as? String) {
                   segueNames.append(identifier)
                }
                else {
                    segueNames.append("no identifier for \(template)")
                }
            }
    
            return segueNames as! Array<AnyHashable>
        }
    }
    

    I know my use of predicates could be better, but Swift is such a PITA when dealing with iterating arrays. Please feel free to improve this.

    0 讨论(0)
提交回复
热议问题