NSInvalidArgumentException异常求助:objects[0]插入nil但initialController非空
Hey there! Let’s dig into this crash you’re hitting—even though you’ve confirmed your initial controller isn’t nil, that attempt to insert nil from objects[0] error tells us somewhere in your data source logic, a nil view controller is slipping into the UIPageViewController’s internal array. Here are the most likely spots to check:
Double-check your
viewControllerBeforeViewController:andviewControllerAfterViewController:methods
These methods should return a valid UIViewController when there’s a previous/next page, or nil only when there are no more pages to show. But if you’ve got a logic gap here—like returning nil when you actually should have a controller, or accidentally creating a controller that ends up nil (think: a typo in a nib name when usinginitWithNibName:bundle:)—the page view controller will try to insert that nil into its array, triggering the crash.
For example, this risky code could cause the issue if your storage array has a nil entry or you don’t do bounds checking:- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { NSInteger index = [self.pageControllers indexOfObject:viewController]; // No bounds check, or pageControllers has a nil entry return self.pageControllers[index + 1]; }Fix: Add strict bounds checking and make sure every entry in your controller storage array is non-nil.
Audit your controller storage array
If you’re using an array (likeself.pageControllers) to hold all the view controllers for the page view, scan every entry to ensure none are nil. Your initial controller might be valid, but another controller in the array could be uninitialized or failed to instantiate—when the page view controller tries to load it during scrolling, you’ll hit this crash.Match
presentationCountForPageViewController:to your actual controller count
If the number returned here doesn’t line up with the real number of valid controllers you have, the page view controller might request a controller that doesn’t exist. For example, if you return4here but only have 3 working controllers, when it asks for the 4th, your data source method could return nil, leading to the crash.Debug controller instantiation
If you’re creating controllers on-demand (instead of preloading all upfront), add breakpoints or logs in the code where you instantiate each controller. Common pitfalls here include:- A typo in a storyboard identifier when using
instantiateViewControllerWithIdentifier: - A missing nib file when initializing with
initWithNibName:bundle: - Forgetting to properly initialize a custom controller (like skipping
[super init...]in your init method, resulting in a nil instance)
- A typo in a storyboard identifier when using
One quick pro tip: Set a breakpoint on NSInvalidArgumentException—this will catch the crash exactly when it happens, showing you the call stack and pinpointing which method is returning the nil controller. That’ll save you a ton of guesswork!
内容的提问来源于stack exchange,提问作者Alk

