crash after pushing a child view controller onto a UINavigationController - objective-c

I'm trying to use the following code to push a new view controller on the stack:
RootViewController *childViewController = [[RootViewController alloc] init];
// initialize childViewController fields
[self.navigationController pushViewController:childViewController animated:YES];
When I do this I get the following crash. Can someone please help me figure out why?
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 1'
*** Call stack at first throw:
(
0 CoreFoundation 0x00db0be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f055c2 objc_exception_throw + 47
2 CoreFoundation 0x00daa7f1 -[__NSArrayM insertObject:atIndex:] + 225
3 CoreFoundation 0x00da5c44 -[__NSArrayM addObject:] + 68
4 UIKit 0x003123fd -[UINavigationBar pushNavigationItem:] + 210
5 UIKit 0x00312947 -[UINavigationBar _pushNavigationItem:transition:] + 124
6 UIKit 0x003778f0 -[UINavigationController pushViewController:transition:forceImmediate:] + 1110
7 UIKit 0x003704a0 -[UINavigationController pushViewController:animated:] + 62
...

The problem is that the UINavigationItem is nil for that controller. I had the same exact crash and it was because I was returning a custom navigation item that had not yet been set. I solved it by returning super's navigationItem instead if it was nil:
- (UINavigationItem *) navigationItem
{
if (navItem) {
return navItem;
}
return [super navigationItem];
}

Related

App is fails when I try set customInfoViewController to nil

CustomInfoViewController property of AVPlayerViewController is nil by default.
https://developer.apple.com/documentation/avkit/avplayerviewcontroller/2868498-custominfoviewcontroller?language=objc
When I set new ViewController as custom all is fine:
UIViewController* vc = [[UIViewController alloc] init];
vc.preferredContentSize = CGSizeMake(1000, 100);
vc.title = #"TEST";
self.playerViewController.customInfoViewController = vc; // playerViewController is AVPlayerViewController
But when I try to set customInfoViewController back to nil (to hide it) - it always fails:
self.playerViewController.customInfoViewController = nil;
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010746ea56 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x0000000106537031 objc_exception_throw + 48
2 CoreFoundation 0x00000001074ae54c _CFThrowFormattedException + 194
3 CoreFoundation 0x000000010737bccd -[__NSPlaceholderArray initWithObjects:count:] + 237
4 CoreFoundation 0x0000000107382694 +[NSArray arrayWithObjects:count:] + 52
5 AVKit 0x000000010723997c -[AVPlayerViewController(AVPlayerViewControllerControls) setCustomInfoViewController:] + 88
Any suggestions how I can hide customInfoViewController?
Settings a property to nil will not hide it!
Depends on how you presented CustomInfoViewController.
If you presented it using present function, you should call dismiss and set it to nil inside the completion block.
if you presented it by simply adding its view to AVPlayerViewController's view, you need to remove it from its superview (using removeFromSuperview()) and only then, set it to nil.

Exception : *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle

I am trying to use .xib files in iOS10+ in table View Controller again and again to load
static NSString *reuseIdentifier = #"SessionTableViewCellIdentifier";
SessionTableViewCell *cell = (SessionTableViewCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if( !cell )
{
cell = [[[NSBundle mainBundle] loadNibNamed:#"SessionTableViewCell" owner:self options:nil] objectAtIndex:0];
}
It is working fine on Version 9.3 but after some time it gives me exception on iOS 10+
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/srtekappledemo/Library/Developer/CoreSimulator/Devices/1848F1BA-50B8-41AB-8538-CD741BD1A685/data/Containers/Bundle/Application/391AD08C-1DD7-46A0-A4D0-A07274A72241/ACSuccess.app> (loaded)' with name '3pB-ff-s9x-view-jo5-RS-cHl' and directory 'Main.storyboardc''
*** First throw call stack:
(
0 CoreFoundation 0x000000010e08fd4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010d6d021e objc_exception_throw + 48
2 CoreFoundation 0x000000010e0f92b5 +[NSException raise:format:] + 197
3 UIKit 0x000000010f342bd2 -[UINib instantiateWithOwner:options:] + 507
4 UIKit 0x000000010f0d69c5 -[UIViewController _loadViewFromNibNamed:bundle:] + 386
5 UIKit 0x000000010f0d72e7 -[UIViewController loadView] + 177
6 UIKit 0x000000010f0d761c -[UIViewController loadViewIfRequired] + 201
7 UIKit 0x000000010f0de062 -[UIViewController __viewWillAppear:] + 118
8 UIKit 0x000000010f1091d3 -[UINavigationController _startCustomTransition:] + 1290
9 UIKit 0x000000010f119e48 -[UINavigationController _startDeferredTransitionIfNeeded:] + 697
10 UIKit 0x000000010f11afdb -[UINavigationController __viewWillLayoutSubviews] + 58
11 UIKit 0x000000010f311dd7 -[UILayoutContainerView layoutSubviews] + 223
12 UIKit 0x000000010effaab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
13 QuartzCore 0x000000010d02ebf8 -[CALayer layoutSublayers] + 146
14 QuartzCore 0x000000010d022440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
15 QuartzCore 0x000000010d0222be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16 QuartzCore 0x000000010cfb0318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
17 QuartzCore 0x000000010cfdd3ff _ZN2CA11Transaction6commitEv + 475
18 UIKit 0x000000010ef607f0 _afterCACommitHandler + 346
19 CoreFoundation 0x000000010e034267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20 CoreFoundation 0x000000010e0341d7 __CFRunLoopDoObservers + 391
21 CoreFoundation 0x000000010e018f8e __CFRunLoopRun + 1198
22 CoreFoundation 0x000000010e018884 CFRunLoopRunSpecific + 420
23 GraphicsServices 0x0000000111302a6f GSEventRunModal + 161
24 UIKit 0x000000010ef35c68 UIApplicationMain + 159
25 ACSuccess 0x000000010cbc08ff main + 111
26 libdyld.dylib 0x0000000111a7668d start + 1
but there are so many links for it I have tried almost everything, Stated below:
Have you renamed your xib or storyboard file outside Xcode? If so, check everywhere you have a reference to it for inconsistencies. Also be sure you respect the uppercase / lowercase letters anywhere you reference the file since is case sensitive.
Go to Interface Builder, select your View1 ViewController > Utilities Panel > Identity Inspector > check if the Custom Class corresponds to your ViewController Class if you have one (.h and .m files).
Check your target's Build Phases > Copy Bundle Resources and make sure that your xib or storyboard file is added there. If you instantiate your View1 ViewController programmatically and you use xib file check the nib name for typos (remember, is case-sensitive). Also, if you currently append .xib file format to the name, remove the extension because it shouldn't be used. What I mean is this line of code:
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"xibFileName" bundle:nil];
Check the properties of your xib or storyboard file in the file inspector and make sure that your file is linked to your target in the Target Membership selection panel.
Check the properties of the xib or storyboard file in the file inspector and try switching your file Location to Relative to project or Relative to group. See if either way makes a difference.
If you're adding your view controller programmatically in the initWithCoder then you should instantiate it in the viewDidLoad method instead
If nothing from above applies to your situation, you could delete the file from Xcode (select Remove References) and import it again in your project.
Check your Info.plist file. If you use storyboads, you should have an entry like Main storyboard file base name and NOT Main nib file base name. If you use nibs, then is vice-versa. Also check that the file base name corresponds with name of the actual file.
Am I missing memory leak which is creating it this query?

Finding line corresponding to error in XCode debugger

When XCode outputs an error like 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]:etc., how can I find the line of code with that objectAtIndex code?
Create a new Exception breakpoint. It will usually show you where the exception is first thrown. Go to the Breakpoints tab on the left:
Look in the lines below it,
you should have a back-trace of the methods that run by you and by the system.
There, you should find the name of the method in which it occurred in.
I've just took an existing project, and add a similar crash and purpose to illustrate this.
My method-
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.contacts = [NSMutableArray arrayWithArray: [ARContact loadContacts]];
NSLog(#"%#", self.contacts[100]); // Index 100 is outside of the array scope
....
}
The console log of the crash (on the bottom of Xcode)
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 100 beyond bounds for empty array'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101a87495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001007ec99e objc_exception_throw + 43
2 CoreFoundation 0x0000000101a2d745 -[__NSArrayM objectAtIndex:] + 213
3 PhoneBookApp 0x0000000100001f95 -[ViewController viewWillAppear:] + 261
4 UIKit 0x0000000100b1adb5 -[UIViewController _setViewAppearState:isAnimating:] + 422
5 UIKit 0x0000000100b38c4d -[UINavigationController _startTransition:fromViewController:toViewController:] + 707
Here you could see a reverse order of things as they occurred (meaning 0 is the latest thing that run, 1 is just before it, etc').
If you would look at numbers 3 and 4, You'll see that before the crash, there is a call to NSArray's objectAtIndex: method (line 3),
And this method called from viewWillAppear: (line 4).

MAC Application crash on button click event

Hello friends I faced problem while creating MAC Application I have two view controllers. when I clicks on button which is placed in first view controller app replace view using below code
- (IBAction)gotoEmployeeView:(id)sender
{
delegate = (AppDelegate *)[[NSApplication sharedApplication]delegate];
secondViewController *employeeVC = [[secondViewController alloc]initWithNibName:#"secondViewController" bundle:nil];
[[delegate.window.contentView animator] replaceSubview:self.view with:secondVC.view];
secondVC.view.frame = ((NSView *)delegate.window.contentView).bounds;
}
now I have one button and I have set click event for this button on second view. below id button click event
- (IBAction)btnClicked:(id)sender
{
NSLog(#"clicked");
}
and when I clicked on it app was crashed. and get below error
2013-08-12 14:33:46.989 Employee Register[1954:303] -[__NSCFString btnClicked:]: unrecognized selector sent to instance 0x10213ace0
2013-08-12 14:33:46.991 Employee Register[1954:303] -[__NSCFString btnClicked:]: unrecognized selector sent to instance 0x10213ace0
2013-08-12 14:33:46.994 Employee Register[1954:303] (
0 CoreFoundation 0x00007fff8d76cf56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff80e47d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8d7f91be -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8d759e23 ___forwarding___ + 371
4 CoreFoundation 0x00007fff8d759c38 _CF_forwarding_prep_0 + 232
5 CoreFoundation 0x00007fff8d75c70d -[NSObject performSelector:withObject:] + 61
6 AppKit 0x00007fff8c5208ca -[NSApplication sendAction:to:from:] + 139
7 AppKit 0x00007fff8c5207fe -[NSControl sendAction:to:] + 88
8 AppKit 0x00007fff8c520729 -[NSCell _sendActionFrom:] + 137
9 AppKit 0x00007fff8c51fbec -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
10 AppKit 0x00007fff8c59fb74 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
11 AppKit 0x00007fff8c51e7f6 -[NSControl mouseDown:] + 786
12 AppKit 0x00007fff8c4e9c98 -[NSWindow sendEvent:] + 6306
13 AppKit 0x00007fff8c4833a5 -[NSApplication sendEvent:] + 5593
14 AppKit 0x00007fff8c419a0e -[NSApplication run] + 555
15 AppKit 0x00007fff8c695eac NSApplicationMain + 867
16 Employee Register 0x0000000100001342 main + 34
17 Employee Register 0x0000000100001314 start + 52
)
It looks like you're sending a btnClicked message to a NSString object. Can you post the code where you call [... bntClicked] ?
The behaviour you are describing can mean either that the viewController implementing the btnClicked method gets deallocated before the action is called or that the action in interface builder is not set correctly.
You should add a log statement in the dealloc method of the viewController implementing the btnClicked and see that it does not get deallocated before the action is called. If you're using ARC make sure that you have a member data pointing to your view controllers so they don't get released.
Also you should delete the ibaction association in interface builder and do it again making sure that you connect the button to the correct view controller.
Your gotoEmployeeView code looks fine to me.
NOTE : replaceSubview:with: method causes oldView to be released.
Take a look at sample project.

Array Initialization

The following code for initialization of array works ::
NSArray *array = [[NSArray alloc] initWithObjects:#"Toy Story 3",#"Inception",nil];
self.list = [array sortedArrayUsingSelector:#selector(compare:)];
[array release];
[super viewDidLoad];
But the following code doesnt. The iPhone Simulator Terminates as soon as I try to scroll the Table View which i used to view the array. (Only after i scroll onto empty tableViewCells)
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:#"MovieList" ofType:#"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
NSLog([array objectAtIndex:1]);
self.list = [array sortedArrayUsingSelector:#selector(compare:)];
[array release];
[super viewDidLoad];
This was an example app from the Book "Beginning iPhone Development" by Dave Mark. In the example , they have initialized the array within the code, while i have tried to initialize it from a external file.
The console Log ::
2010-12-22 20:57:43.772 Nav[2474:40b] WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWithIndexPath: in <RootViewController: 0x9908870>. Please remove your implementation of this method and set the cell properties accessoryType and/or editingAccessoryType to move to the new cell layout behavior. This method will no longer be called in a future release.
2010-12-22 20:58:12.480 Nav[2474:40b] WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWithIndexPath: in <DisclosureButtonController: 0x9b32ab0>. Please remove your implementation of this method and set the cell properties accessoryType and/or editingAccessoryType to move to the new cell layout behavior. This method will no longer be called in a future release.
2010-12-22 20:59:13.299 Nav[2474:40b] -[UIDeviceRGBColor length]: unrecognized selector sent to instance 0x9b3d900
2010-12-22 20:59:13.301 Nav[2474:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceRGBColor length]: unrecognized selector sent to instance 0x9b3d900'
*** Call stack at first throw:
(
0 CoreFoundation 0x00db2be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f075c2 objc_exception_throw + 47
2 CoreFoundation 0x00db46fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d24366 ___forwarding___ + 966
4 CoreFoundation 0x00d23f22 _CF_forwarding_prep_0 + 50
5 UIKit 0x0051a9ca -[UITableViewCellLayoutManager layoutSubviewsOfCell:] + 3424
6 UIKit 0x00482e02 -[UITableViewCell layoutSubviews] + 95
7 QuartzCore 0x01c70451 -[CALayer layoutSublayers] + 181
8 QuartzCore 0x01c7017c CALayerLayoutIfNeeded + 220
9 QuartzCore 0x01c6937c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
10 QuartzCore 0x01c690d0 _ZN2CA11Transaction6commitEv + 292
11 QuartzCore 0x01c997d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
12 CoreFoundation 0x00d93fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
13 CoreFoundation 0x00d290e7 __CFRunLoopDoObservers + 295
14 CoreFoundation 0x00cf1bd7 __CFRunLoopRun + 1575
15 CoreFoundation 0x00cf1240 CFRunLoopRunSpecific + 208
16 CoreFoundation 0x00cf1161 CFRunLoopRunInMode + 97
17 GraphicsServices 0x016e7268 GSEventRunModal + 217
18 GraphicsServices 0x016e732d GSEventRun + 115
19 UIKit 0x002ca42e UIApplicationMain + 1160
20 Nav 0x00002598 main + 102
21 Nav 0x00002529 start + 53
)
terminate called after throwing an instance of 'NSException'
enter code here
It sounds like this is a perfect job for the debugger, doesn't it? Why not set a breakpoint on the first line and make sure nothing is unexpectedly nil or out of bounds as you step through and inspect your variables? Perhaps paying attention to the error that's undoubtedly logged to the console might be helpful, too?
Given you've mentioned neither the line on which it terminates nor any log messages, this is about as specific as anyone can get.
The first argument to NSLog needs to be an NSString. The object you're passing to it appears to be part of the UIColor cluster. I suggest you change to:
NSLog(#"%#", [array objectAtIndex:1]);
So the first argument is definitely a string and it says just to print a description of whichever object is the next argument.