Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何检查视图控制器是否具有指定的标识符_Ios_Swift_Uiviewcontroller - Fatal编程技术网

Ios 如何检查视图控制器是否具有指定的标识符

Ios 如何检查视图控制器是否具有指定的标识符,ios,swift,uiviewcontroller,Ios,Swift,Uiviewcontroller,在我的应用程序中,标识符来自服务器,我们将它们用作视图控制器标识符。这里我想检查具有指定标识符的ViewController是否可用。如果可用,则只按该控制器,否则返回。我已经编写了如下代码 let identifier = Constants.menuSections[indexPath.section-1][indexPath.row-1] if let vc1 = (self.storyboard?.instantiateViewController(withIdentifier:ide

在我的应用程序中,标识符来自服务器,我们将它们用作视图控制器标识符。这里我想检查具有指定标识符的ViewController是否可用。如果可用,则只按该控制器,否则返回。我已经编写了如下代码

let identifier = Constants.menuSections[indexPath.section-1][indexPath.row-1]

if let vc1 = (self.storyboard?.instantiateViewController(withIdentifier:identifier)){
    let navi = BaseNaviViewController(rootViewController:vc1)
    navi.navigationBar.tintColor = .white
    navi.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    sideMenuController?.embed(centerViewController:navi, cacheIdentifier:identifier)

}else {
    return
}
这里我得到一个错误,说

由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“情节提要()不包含标识符为的视图控制器。” “abc”


您可以将所有标识符存储在enum中,如下所示:

enum Identifier: String {
    case viewcontroller

    var storyboardName: String {
        switch self {
        case .viewcontroller : return "Main"
        }
    }
}

if let validIdentifier = Identifier(rawValue: "vc") {
    let storyboard = UIStoryboard(name: validIdentifier.storyboardName, bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: validIdentifier.rawValue)
}

如果标识符不存在,此方法将返回nil,因此只需使用NSAssert检查该值。

如果我没有错,您将尝试根据
情节提要标识符实现动态屏幕重定向,并从数组中获取
情节提要id

早些时候,我处理过同样的需求,也遇到过同样的问题。不幸的是,如果
storyboard\u id
不存在,就没有办法停止崩溃

当时,如果目标ViewController不可用,我添加了一个空的
情节提要id
,在重定向之前,我检查了
情节提要id
是否为空,然后跳过重定向代码

代码:
谢谢

最后,我找到了一个解决方案

extension UIStoryboard {
    func instantiateVC(withIdentifier identifier: String) -> UIViewController? {
        // "identifierToNibNameMap" – dont change it. It is a key for searching IDs 
        if let identifiersList = self.value(forKey: "identifierToNibNameMap") as? [String: Any] {
            if identifiersList[identifier] != nil {
                return self.instantiateViewController(withIdentifier: identifier)
            }
        }
        return nil
    }
}
我使用的方法如下

let identifier = Constants.menuSections[indexPath.section-1][indexPath.row-1]

 if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateVC(withIdentifier: identifier) {
          let navi = BaseNaviViewController(rootViewController:viewController)
                 navi.navigationBar.tintColor = .white
                 navi.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
                 sideMenuController?.embed(centerViewController:navi, cacheIdentifier:identifier)
        }
        else {

            ServerService.ShowAlertMessage(ErrorMessage: "No controller Available", title: "Oops . . . !", view: self)
        }

你能提供NSAssert的详细代码吗?@Anil Method将返回nil->它将使你的应用程序崩溃。Hitesh感谢你的解决方案。在我的情况下,标识符不是空的。但是具有该标识符的控制器在情节提要中不可用。我想你了解我的情况。标识符不是空的/空的:看起来,如果我们提供了不正确的故事板id,那么它将使我们的应用程序崩溃。我尽了一切努力来解决这个问题,但没有成功。
let identifier = Constants.menuSections[indexPath.section-1][indexPath.row-1]

 if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateVC(withIdentifier: identifier) {
          let navi = BaseNaviViewController(rootViewController:viewController)
                 navi.navigationBar.tintColor = .white
                 navi.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
                 sideMenuController?.embed(centerViewController:navi, cacheIdentifier:identifier)
        }
        else {

            ServerService.ShowAlertMessage(ErrorMessage: "No controller Available", title: "Oops . . . !", view: self)
        }