是否可以在实例化对象之前检查故事板中是否存在标识符?

whi*_*eam 14 cocoa-touch objective-c ios uistoryboard

在我的代码中我有这一行,但我想知道是否有办法在我使用" instantiateViewControllerWithIdentifier "方法之前检查@"SomeController"是否存在.如果标识符不存在则应用程序崩溃.

如果没有一个好的方法,这不是一个大问题,我可以更加小心,不要吝啬标识符名称,但我希望我能更优雅地处理它.

UIViewController *newTopViewController = [self.storyboard    instantiateViewControllerWithIdentifier:@"SomeController"];
Run Code Online (Sandbox Code Playgroud)

Var*_*roX 7

正如汤姆所说,这个问题的最佳解决方案是try-catch块:

@try {
        UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"identifier"];

    }
    @catch (NSException *exception) {
        UIAlertView *catchView;

        catchView = [[UIAlertView alloc]
                     initWithTitle: NSLocalizedString(@"Error", @"Error")
                     message: NSLocalizedString(@"Identifier not found on SB".", @"Error")
                     delegate: self
                     cancelButtonTitle: NSLocalizedString(@"OK", @"Error") otherButtonTitles: nil];

        [catchView show];
    }
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助!即使答案真的很晚.


Kev*_*vin 6

你可以valueForKey:UIStoryboards上使用.UIStoryboards有一个名为"identifierToNibNameMap"的键,其值NSDictionary与该UIViewController故事板中的s一致.这个内部NSDictionary使用viewcontroller的名称作为键,因此您可以使用以下代码实际检查故事板中是否存在viewcontroller:

if ([[storyboard valueForKey:@"identifierToNibNameMap"] objectForKey:myViewControllerName]) {
    // the view controller exists, instantiate it here
    UIViewController* myViewController = [storyboard instantiateViewControllerWithIdentifier:myViewControllerName];
} else {
    //the view controller doesn't exist, do fallback here
}
Run Code Online (Sandbox Code Playgroud)

注意: Apple已经知道拒绝使用查询cocoa类的基础属性的应用程序valueForKey:.这些底层属性可能在将来的任何时候发生变化,在没有警告的情况下破坏应用功能 这些东西没有弃用过程.


dze*_*sik 6

@Kevin 的解决方案有效。这是我在代码中使用的 Swift 3 作为函数的一段非常相同的代码:

func instantiateViewController(fromStoryboardName storyboardName: String, withIdentifier identifier: String) -> UIViewController? {
    let mainStoryboard = UIStoryboard(name: storyboardName, bundle: nil)
    if let availableIdentifiers = mainStoryboard.value(forKey: "identifierToNibNameMap") as? [String: Any] {
        if availableIdentifiers[identifier] != nil {
            if let poiInformationViewController = mainStoryboard.instantiateViewController(withIdentifier: identifier) as? UIViewController {
                return viewController
            }
        }
    }
    return nil
}
Run Code Online (Sandbox Code Playgroud)

使用此功能如下:

if let viewController = self.instantiateViewController(fromStoryboardName: "YourStoryboardName", withIdentifier: "YourViewControllerStoryboardID") {
    // Here you are sure your viewController is available in the Storyboard
} else {
    print("Error: The Storyboard with the name YourStoryboardName or the Storyboard identifier YourViewControllerStoryboardID is not available")
}
Run Code Online (Sandbox Code Playgroud)


bor*_*den 3

不,没有对此进行检查。但是,您不需要这样做。如果标识符不存在,此方法将返回nil,因此只需使用NSAssert.

编辑实际上这是错误的!这很奇怪...文档的返回值部分与另一部分相矛盾...但答案最终是否定的(没有方法来检查标识符是否存在)