【问题标题】:Swift: programmatically enumerate outgoing segues from a UIVIewControllerSwift:以编程方式枚举来自 UIVIewController 的传出 segue
【发布时间】:2016-01-28 11:25:21
【问题描述】:

我想列出来自 UIViewController 的传出 segue,如 Programmatically enumerate outgoing Segues for a UIViewController 中所述,但在 Swift 中。 (Swift 2、Xcode 7、iOS8+)。

我可以的

override func viewDidLoad() {
    super.viewDidLoad()
    let s = valueForKey("storyboardSegueTemplates")
    print("switchingVC: segues: \(s)") 
}

这会产生类似的输出

switchingVC: segues: Optional((
    "<UIStoryboardPresentationSegueTemplate: 0x1754a130>",
    "<UIStoryboardPresentationSegueTemplate: 0x17534f60>",
    "<UIStoryboardPresentationSegueTemplate: 0x17534fc0>"
))

但在那之后我很难产生任何东西。我找不到UIStoryboardPresentationSegueTemplate 的任何定义。我怎样才能说服 Swift 告诉我里面有什么?如何找到转场identifier

谢谢!

【问题讨论】:

    标签: swift uistoryboardsegue


    【解决方案1】:

    这个 valueForKey("storyboardSegueTemplates")UNDOCUMENTED 属性,UIStoryboardPresentationSegueTemplateUNDOCUMENTED 类。如果您将应用程序上传到 App Store,请注意 App Store 的拒绝。

    如果您想在内部项目中使用它,请按以下方式使用

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

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

    【讨论】:

    • 太棒了!这行得通,非常感谢,尤其是 github 链接。
    • 它可能有效,但要注意 App Store 审批流程
    【解决方案2】:

    根据 Swift 4.2 和 https://stackoverflow.com/a/35060917/1058199。谢谢/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>
        }
    }
    

    我知道我对谓词的使用可能会更好,但是在处理迭代数组时,Swift 就是这样一个 PITA。请随时改进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多