开发者

Why won't this view controller init? (NEVER MIND)

In trying to debug why a view controller is initing empty, I've ended up a sort of weird plac开发者_JAVA技巧e. Check this out:

OffersSearchController *searchController = [[OffersSearchController alloc]
        initWithNibName:@"This is a completely bogus nib name."
             bundle:nil];

Not a single complaint. I've seen that sort of construct crash out with complaints about being unable to find a nib named "This is a completely bogus...", but not this time. Instead, my searchController pushes onto the navigation controller as if it had loaded successfully. It's empty, though--I can see the full screen of another view that's (accidentally!) "underneath" my UINavigationController stack.

What's happening here? Is [OffersSearchController alloc] coming back nil for some reason?

EDIT: Never mind. Here's the lesson: don't implement loadView when you mean to implement viewDidLoad. Oy. Long week.


Here's the answer (thanks @Eric Petroelje for suggesting I post and accept the answer).

In a burst of late-Friday-afternoon productivity, moving far faster than is recommended, I set up my property initializers and picker-wheel-data-source arrays in -(void)loadView rather than in -(void)viewDidLoad.

Rather than the initWithNibName: call's call to loadView being allowed to propagate up to UIViewController, it happily initialized my fields and that's all.


The documentation for UIViewController initWithNibName:bundle doesn't say anything about what happens if the specified nib name is invalid. Presumably then, an invalid nib name is treated the same as a nil one. However, it does say that the return value is always an initialized UIViewController.

So, what that code is doing is to allocate/initialize a new OffersSearchController with no nib. The view appears empty because it is. You've probably never actually seen that crash before, because it's not supposed to; what's happening is perfectly normal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜