setRightBarButtonItems:animated: crash - ios

I called
[self.navigationItem setRightBarButtonItems:nil animated:YES];
to hide the buttons during refresh operations
Sometimes I caught an exception saying
*** setObjectForKey: object cannot be nil (key: kUINavigationBarAnimationRightViews)

Previously the rightBarButtonItems array contained an item with a custom view - a UISearchBar being a firstResponder.
Calling the following line of code caused the crash
[self.navigationItem setRightBarButtonItems:nil animated:YES];
The solution
Simply calling
self.searchBar resignFirstResponder];
solved the issue

Related

UIRefreshControl addTarget action causing "unrecognized selector" error

I have a UITableViewController in which I am implementing a UIRefreshControl for pull to refresh. Everything is working fine, the table is getting populated from my web service. But when I pull down to refresh I get the error:
[MyViewController refreshView]: unrecognized selector sent to instance ...
Which is complaining about the addTarget action here:
UIRefreshControl * refresh = [[UIRefreshControl alloc] init];
[refresh addTarget:self action:#selector(refreshView) forControlEvents:UIControlEventValueChanged];
The error flag on that line in the editor is Undeclared selector 'refreshView'
My refreshView method is simply:
- (void) refreshView: (UIRefreshControl *)refresh {
NSLog(#"test");
}
Any ideas as to why this would be causing the application to crash? (I am running iOS 7.1)
If you declared your method as "refreshView:" (i.e. with a parameter), you need to add a colon to the "#selector" bit.
In other words, one line changes with one character:
[refresh addTarget:self action:#selector(refreshView:) forControlEvents:UIControlEventValueChanged];

[_UIWebViewScrollViewDelegateForwarder scrollViewWasRemoved:]: unrecognized selector sent to instance

I've a really strange error, and I cannot find the problem.
In my iPad APP I've a UINavigationController, a UITableViewController as master and a UIViewController containing a UIWebView as detail.
I launch the APP, the UITableViewController is shown. By segue I open the detail as usual. Then I have a back button in my detailviewcontroller that calls this method
[self.contentWebView setDelegate:nil];
[self.contentWebView stopLoading];
[self.navigationController popViewControllerAnimated:YES];
It gets poped and the master is shown again. Its
- (void)viewWillAppear:(BOOL)animated
gets called, but then I get the following error:
2014-06-06 15:56:58.156 Knowledge[356:60b] -[_UIWebViewScrollViewDelegateForwarder scrollViewWasRemoved:]: unrecognized selector sent to instance 0x170429f60
2014-06-06 15:56:58.159 Knowledge[356:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIWebViewScrollViewDelegateForwarder scrollViewWasRemoved:]: unrecognized selector sent to instance 0x170429f60'
*** First throw call stack:
(0x189582f50 [...] 0x196553aa0)
libc++abi.dylib: terminating with uncaught exception of type NSException
BUT, this happens only on the iPad Air... older iPads work as expected
UPDATE:
I added now the command [self.contentWebView removeFromSuperview]; after "stopLoading" and now the error is happing right up there.
Please ignore this answer if you are not touching scroll view's (of the web view) delegate reference.
I was facing the same strange error. After some investigation, I found that it was happening because scroll view's (of the web view) delegate was altered in controller's viewDidLoad method:
[self webView].scrollView.delegate = self;
In some cases, scroll view invokes delegate method(s) even after your view controller is destroyed. This should be prevented by weak delegate reference, but for some reason scroll view calls delegate method on deallocated (zombie) object anyway. I assume this is a bug in the framework.
So here what you can do: set delegate reference to nil in your controller's dealloc method:
- (void)dealloc
{
[self webView].scrollView.delegate = nil;
}
After deallocation, scroll view will be prevented from calling any method on no longer existing controller.

"Array out of bounds" error when trying to add subview in iOS

I'm currently writing an app and I'm experiencing a problem that seems to be caused by code that I did not write (i.e., Apple's classes). I create an instance of a subclass of UIViewController that I wrote. I add it as a child view controller of another custom view controller. Then, when I try to add this new view controller's view as a subview of the parent view controller's view I get a crash with this error.
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
I have tested and determined that the problem is specifically cause by trying to add the view as a subview. I reference the view in an NSLog just to make sure that it isn't simply the act of referencing it that's causing the error. I've tried adding the view as a subview of different views and that also crashed, so the problem is not with the parent view. Finally, I have tried to add a different view as a subview to the parent view and that did work, further proving that the parent view is fine, and that the prospective subview is at fault. The code where I allocate it is this:
ScheduleSelectorViewController* selector = [[ScheduleSelectorViewController alloc] initWithNibName:#"ScheduleSelectorViewController" bundle:nil];
This has always worked for me. I don't know what it is I've changed. I don't know enough about the inner workings of subview hierarchy to know which array is empty and is causing this crash, so if anyone can help me out I would be extremely grateful.
If there's any other information I could supply that would help let me know.
UPDATE:
Here is the code where it crashes. I have placed NSLogs to indicate the line at which it breaks.
- (void) addViewControllerToStack:(UIViewController *)controller withKey:(NSString *)key
{
if ( !self.stack ) {
self.stack = [[NSMutableDictionary alloc] init];
}
NSLog(#"subviews %#", [controller.view subviews]);
[[controller view] setFrame:offScreenFrame];
[self addChildViewController:controller];
NSLog(#"code gets to here");
[self.view addSubview:controller.view];
NSLog(#"but not to here");
[self.view bringSubviewToFront:controller.view];
[self.stack setObject:controller forKey:key];
[self.stackKeys addObject:key];
}
For the record, the subviews array is not nil.
Check if you have set view's.hidden = YES; somewhere
I was pulling my hair for more than hour to find out I was hiding my pager control and try to set the pageIndicatorTintColor property later which also throw array out of bounds issue.
Make sure you did not override init/ initWithNibName methods in ScheduleSelectorViewController without calling the super methods.
And of course you can always print out [self.view subviews] for parent view in console to understand if subview array is whether nil. If it is nil you should initiate it before adding any views.

How to remove all the UIPopOver references of my app on idle time exceed

Im setting an MaxIdleTime in my app once if no userInteraction happened within this specified time i'm just removing the existing view from my window and adding my login (Home) view as subview to my apps UIWindow through a method called logout.
In this logout i'm just removing all the references which are alive but if any of the UIPopOverController is visible on any view during this logout call i'm getting exception
-[UIPopoverController dealloc] reached while popover is still visible.
Im making popover instance to nil in viewDidUnload even though i'm getting this exception and app is crashing and my project is ARC enabled.
How to overcome this exception, any help is appreciated in advance.
To remove all the subviews in your main view when the timer fires, use the following code:
for (UIView *sub in [[_view subviews] copy]) {
[sub removeFromSuperView];
sub = nil;
}
As for the popovers, simply remove the one currently onscreen with:
[pop dismissPopoverAnimated:NO];
pop = nil;

Dismissing a modal view causes the app crash

I'm displaying a modal view called "rule" from a round rect button. In that "rule" modal view i'm displaying another modal view called "newRule" when user clicks the Create Rule button.
When i'm quitting from the "newRule" modal view the app crashes. Here's the code i had written for quitting the "newRule" modal view.
[self dismissModalViewControllerAnimated:YES];
Nothing is displayed in the console. When i tried to debug the code, it displayed a EXC_BAD_ACCESS after the dealloc method. My dealloc method looks like this:
[label release];
label = nil;
[imageArray release];
imageArray = nil;
[languageElementsArray release];
languageElementsArray = nil;
[super dealloc];
Please help me.
Is the label a UILabel object? Also what are in the arrays? Views are automatically released once their superview is released, so releasing a subview after its superview has been released (or releasing the subview then the superview) will cause an crash similar to the one you describe
I'm experiencing something similar. When I comment out the last line ( [super dealloc] ), it then works. Does this make a difference for you?
If you happen to be using Automatic Reference Counting in Xcode 4.2, then you should not have a [super dealloc] at all—which would result in this error.
Of course, in that context you likely should not be releasing these other objects either.

Resources