Failed to render instance of ClassName: The agent threw an exception loading nib in bundle

前端 未结 2 1630
慢半拍i
慢半拍i 2020-12-31 03:50

When I include my custom IBDesignable view in a storyboard or another nib, the agent crashes and throws an exception because it can\'t load the nib.

e

相关标签:
2条回答
  • 2020-12-31 03:53

    The same viewpoint with "Josh Heald", We can't pass nil for bundle. And this one for who in object - C:

    - (UIView *) loadViewFromNib{
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:bundle];
    UIView *v = [[nib instantiateWithOwner:self options:nil]objectAtIndex:0];
    return v;
    }
    
    0 讨论(0)
  • 2020-12-31 04:11

    When Interface Builder renders your IBDesignable views, it uses a helper app to load everything. The upshot of this is that the mainBundle at design time is related to the helper app, and it's not your app's mainBundle. You can see that the path mentioned in the error has nothing to do with your app:

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays

    When loading the nib, you're relying on the fact that passing bundle: nil defaults to your app's mainBundle at run time.

    let nib = UINib(nibName: String(describing: StripyView.self), bundle: nil)
    

    So instead, you need to pass the correct bundle here. Fix the above line with the following:

    let bundle = Bundle(for: StripyView.self)
    let nib = UINib(nibName: String(describing: StripyView.self), bundle: bundle)
    

    That will make Interface Builder load your nib from the same bundle as your custom view class.

    This applies to anything that's loaded from a bundle by your custom view. For example, localised strings, images, etc. If you're using these in your view, make sure you use the same approach and explicitly pass in the bundle for the custom view class.

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