OGeek|极客世界-中国程序员成长平台

标题: iphone - 更早地检测有问题的 XIB View [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:15
标题: iphone - 更早地检测有问题的 XIB View

我的 nib 名称中有一个拼写错误,后来在我推送到导航 Controller 时它在代码中爆炸了。弄清楚它并没有花太长时间,但我认为最好早点断言格式良好,以便更容易弄清楚。问题是它不是零,它只是不能从 Nib 正确地形成。在 initWithNib 之后是否有更好的断言或捕获来检查代码中的早期问题?

//
// typo in nib name - I want to catch before it blows up inside of pushViewController to narrow the problem
//
ENPurchaseDetailView *purchaseView = [[ENPurchaseDetailView alloc] 
    initWithNibName"XibWithTypoInIt" bundle:nil];
assert(purchaseView != nil);  // does not catch - it's not nil - just not well formed

// other code ...

// blows up with sigabort inside of pushViewController
[[self navigationController] pushViewController:purchaseView animated:YES];    



Best Answer-推荐答案


创建一个基础 UIViewController 类,您可以从该类继承所有其他 VC。然后,您可以在基类中执行许多有用的断言,而不必将它们分散在整个代码中。这是我使用的一个

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil {
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]

// never do this stuff in production code
#ifdef DEBUG
    // make sure you didn't fat finger Xib name
    if (nibNameOrNil)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:nibNameOrNil ofType"nib"];
        NSAssert(path, @"Nib %@ does not exist", nibNameOrNil);
    }

    // make sure there's no problems with the Xib
    UINib *nib = [UINib nibWithNibName:nibNameOrNil bundle:nibBundleOrNil ];
    NSArray *instantiatedObjects = [nib instantiateWithOwner:nil options:nil];
    NSAssert(instantiatedObjects && [instantiatedObjects count] > 0, @"Not well formed Xib");
#endif       return self;
}

关于iphone - 更早地检测有问题的 XIB View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7486373/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4