Prevent memory leak my only adding the observer when the searchfield is added to a superview, that way we can also remove the observer later.

This commit is contained in:
Randall Luecke committed 2011-03-17 01:31:25 -04:00
1 parent d5b2ccbc30
commit 32fc629c33
1 file changed
+13 -1
+13 -1
View File
@@ -111,7 +111,7 @@ var RECENT_SEARCH_PREFIX = @" ";
[self setEditable:YES];
[self setContinuous:YES];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_searchFieldTextDidChange:) name:CPControlTextDidChangeNotification object:self];
var bounds = [self bounds],
cancelButton = [[CPButton alloc] initWithFrame:[self cancelButtonRectForBounds:bounds]],
@@ -126,6 +126,18 @@ var RECENT_SEARCH_PREFIX = @" ";
_canResignFirstResponder = YES;
}
- (void)viewWillMoveToSuperview:(CPView)aView
{
[super viewWillMoveToSuperview:aView];
// In order to prevent a memory leak we add the observer here instead of _init, since it doesn't hurt to add it more than once
// but more importantly we remove it if the view is removed from the superview.
if (aView)
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(_searchFieldTextDidChange:) name:CPControlTextDidChangeNotification object:self];
else
[[CPNotificationCenter defaultCenter] removeObserver:self name:CPControlTextDidChangeNotification object:self];
}
// Managing Buttons
/*!
Sets the button used to display the search-button image