UIKeyboardDidHideNotification error application crashing - iphone

I'm using a UIView. The application is using ARC. UIView is used in multiple view controllers. In UIView a listener to the UIKeyboardDidHideNotification is added. The listener works fine with some view controllers and with other view controllers it crashes the application. Especially when I use in the second view contoller after using in the first. The error is
* -[appname keyboardWillHide]: message sent to deallocated instance 0xb9c2760
In some scenarios the listener is getting called twice.
The code i have added in the uiview drawrect method is:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
the code for listener is
-(void)keyboardWillHide
{
if(boolisViewlifted)
{
CGRect newFrame=self.frame;
newFrame=CGRectMake(self.frame.origin.x, self.frame.origin.y+250, self.frame.size.width, self.frame.size.height);
self.frame=newFrame;
boolisViewlifted=false;
}
}
The uiview appears on top of the calling view controller. Please let me know what causes this error and how to solve it.

Your view is getting unloaded because of memory warnings. You need to override dealloc method & remove observer for all notifications in all views where you added observer for notifications.
//do add in all views
-(void)dealloc
{
//[super dealloc];//As you are using ARC don't call super's dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Related

Message Sent to Deallocated instance when going to background

I am using ARC and testing on iOS7 (iPhone 5S).
I have a navigation controller (a) with a pushed UIViewController (b). b presents a modal NavigationViewController (c) via a manual segue. c then pushes a UIViewController (d).
So now we have 2 navigation stacks, one root and one modal.
I would like to dismiss the modal NavigationViewController (c) and all of its children whenever I go to background.
In UIViewController (b) I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(enteredBackground:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
}
-(void)enteredBackground:(NSNotification *)notification{
if (self.presentedViewController) {
[self dismissViewControllerAnimated:NO completion:nil];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
This works fine if I am on C's rootViewController when I go to background, but crashes when I am on D.
2013-10-20 22:49:49.008 MyApp[2596:60b] *** -[UIView release]: message sent to deallocated instance 0x17036aa40
How do I fix this? It seems like a view in the modal navigation stack is being sent dealloc and is already released. After looking in the debugger, the modal nvaigationcontroller (c) itself seems to still exist.
i have made an test sample and checked the same scenario , it is'nt crashing anywhere . Please check for any releases on object of type UIView in your ViewController C and D. Or can you post the code where you are doing all the stuff you mentioned above .

dismiss a modalviewcontroller when the application enters background

i need to dismiss my uiimagepicker modal viewcontroller automatically when the application enters the background.i tried to put the code to dismissmodalviewcontroller code in viewdiddissappear method,but its not being called.so i made a reference to the viewcontroller in appdelegate and tried to put it in the applicationdidenterbackgroundmethod but still it is not working.can someone point out the right way to do this
Try to add an NSNotificationCenter observer for UIApplicationDidEnterBackgroundNotification in the UIViewController that you want to dismiss. Use the selector to dismiss the modalview
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(didEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver: self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)didEnterBackground:(NSNotification*)note
{
[self.navigationController dismissModalViewAnimated:NO];
}
Best way to remove the modal when app is moving to background and it works fine .
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(dismissView:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)dismissView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Also you can remove observer like this
[[NSNotificationCenter defaultCenter] removeObserver: self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
I don't think you need to go through all that.
From the docs:
If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.
Try calling [self dismissModalViewController:NO] from the parent view controller in your implementation of - (void) viewDidUnload.
This is untested, but the docs imply that it should do the job for you.

How to remove an observer for NSNotification in a UIView?

I've added an observer in a custom UIView I've created under initWithFrame:.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateZipFromLocation:)
name:#"zipFoundFromLocation"
object:nil];
The problem is, this view is a subview. When the view is loaded again, it calls the initWithFrame message again, thus adding two observers and so on. How can I remove the observer when the view is going to disappear? Since it is a UIView, it says that viewWillDisappear:(BOOL)animated is not a valid method. Any ideas?
You've said that initWithFrame: is being called more than once, so I assume this means that the view is being destroyed and recreated. You can remove the view as an observer in dealloc, which will be called when the view is no longer retained by anyone:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}

Is there a way to catch an WillRotateToInterfaceOrientation event from an UIView?

every UIViewController has a method called willRotateToInterface.
Is it possible to do this within a UIView too?
Does this match the idea of model view controller ?
The only way I can think of is to send the event from the UIViewController to the UIView.
Is there a global variable for the current orientation?
Observe UIDeviceOrientationDidChangeNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
...
- (void)orientationDidChange:(NSNotification *)note
{
NSLog(#"new orientation = %d", [[UIDevice currentDevice] orientation]);
}
UIDevice Class Reference
I should note that yo uneed to add -beginGeneratingDeviceOrientationNotifications when you want these notifications to be sent, and call -endGeneratingDeviceOrientationNotifications when you want them to stop. There is a battery impact of generating these, so you should only do so when your view is on screen. UIViewController does all this for you, so if you have a view controller, it is worth letting it do the work.
If you just want to adjust view to new size/layout when orientation changes, you should just override its layoutSubviews method.
It will be called whenever size of the view changes, which usually happens when view is rotated.
You can subscribe to a global notification and get a call when the device is rotated, it wont do anything for you though..
[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification" object:nil];

Activity indicator in a UITabBar application on the iPhone

I have a UITabBar + UINavigationController application which often needs data from the internet. Sometimes it takes quite a while before it gets it, so I would like to show an activity indicator.
What I was trying is to add a activityView to my window in my applicationDidFinishLaunching method:
[window addSubview:tabBarController.view];
fullscreenLoadingView.hidden = YES;
[window addSubview:fullscreenLoadingView];
And then I add the application delegate as a observer to the default notification center:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(startFullscreenLoading:) name:#"startFullscreenLoading" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(stopFullscreenLoading:) name:#"stopFullscreenLoading" object:nil];
and implement the methods:
- (void)startFullscreenLoading:(NSNotification *)notification {
fullscreenLoadingView.hidden = NO;
}
- (void)stopFullscreenLoading:(NSNotification *)notification {
fullscreenLoadingView.hidden = YES;
}
When I then use this directly in the applicationDidFinishLaunching method the loading indicator view shows upp as expected:
[[NSNotificationCenter defaultCenter] postNotificationName:#"startFullscreenLoading" object:self];
But when I use it from one of the navigation controllers the startFullscreenLoading: method is called but I don't see the loading indicator view. Why is that?
Are you loading your data on a background thread? Your main thread will be blocked & won't be able to redraw or process any other events.
The most likely thing I can guess is that your indicator view is below some other view. Try
[[fullScreenLoadingView superView] bringSubviewToFront:fullScreenLoadingView]
If that doesn't work, I would suggest breaking inside of -startFullscreenLoading to make sure the fullscreenLoadingView is still valid.
In my app I did this by adding the UIActivityView in IB and making sure it was above everything else (as a top-level object). It's set to be invisible when stopped.
Then in my code I make it appear with [activityIndicator startAnimating] and make it disappear with [activityIndicator stopAnimating];