之前检测有问题的XIB视图 [英] Detecting problematic XIB views earlier

查看:89
本文介绍了之前检测有问题的XIB视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的笔尖名称中有一个拼写错误,当我推送到导航控制器时,它在代码中爆炸了。它没有花太长时间来搞清楚,但我认为最好先断言好形象以便更容易理解。问题是它不是零,它只是无法从笔尖正确形成自己。在initWithNib之后是否有更好的断言或捕获来检查代码中的问题?

I had a typo in my nib name and it was blowing up later in code when I was pushing to the navigation controller. It didn't take too long to figure it out but I thought it would be better to assert well formed earlier to make it easier to figure out. The problem is it's not nil, it just couldn't form itself properly from the nib. Is there a better assert or catch to check after initWithNib to catch problems earlier in code?

//
// 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];    


推荐答案

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

Create a base UIViewController class that you subclass all your other VCs from. Then you can do a number of useful assertions in the base class and not have to scatter them throughout your code. Here is one that I use

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)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;
}

这篇关于之前检测有问题的XIB视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆