Skip to content

Commit 4bddda9

Browse files
author
Ignacio Romero Zurbuchen
committed
Uses the ivar getters for certain lazily created properties. This should fix retain cycle issues.
1 parent c309985 commit 4bddda9

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

Source/SLKTextViewController.m

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,17 @@ - (BOOL)isPresentedInPopover
357357

358358
- (SLKTextView *)textView
359359
{
360-
return self.textInputbar.textView;
360+
return _textInputbar.textView;
361361
}
362362

363363
- (UIButton *)leftButton
364364
{
365-
return self.textInputbar.leftButton;
365+
return _textInputbar.leftButton;
366366
}
367367

368368
- (UIButton *)rightButton
369369
{
370-
return self.textInputbar.rightButton;
370+
return _textInputbar.rightButton;
371371
}
372372

373373
- (UIModalPresentationStyle)modalPresentationStyle
@@ -655,10 +655,10 @@ - (void)textDidUpdate:(BOOL)animated
655655
return;
656656
}
657657

658-
CGFloat inputbarHeight = self.textInputbar.appropriateHeight;
658+
CGFloat inputbarHeight = _textInputbar.appropriateHeight;
659659

660-
self.textInputbar.rightButton.enabled = [self canPressRightButton];
661-
self.textInputbar.editorRightButton.enabled = [self canPressRightButton];
660+
_textInputbar.rightButton.enabled = [self canPressRightButton];
661+
_textInputbar.editorRightButton.enabled = [self canPressRightButton];
662662

663663
if (inputbarHeight != self.textInputbarHC.constant)
664664
{
@@ -669,11 +669,13 @@ - (void)textDidUpdate:(BOOL)animated
669669

670670
BOOL bounces = self.bounces && [self.textView isFirstResponder];
671671

672+
__weak typeof(self) weakSelf = self;
673+
672674
[self.view slk_animateLayoutIfNeededWithBounce:bounces
673675
options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionLayoutSubviews|UIViewAnimationOptionBeginFromCurrentState
674676
animations:^{
675-
if (self.textInputbar.isEditing) {
676-
[self.textView slk_scrollToCaretPositonAnimated:NO];
677+
if (weakSelf.textInputbar.isEditing) {
678+
[weakSelf.textView slk_scrollToCaretPositonAnimated:NO];
677679
}
678680
}];
679681
}
@@ -713,7 +715,7 @@ - (BOOL)canPressRightButton
713715
{
714716
NSString *text = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
715717

716-
if (text.length > 0 && ![self.textInputbar limitExceeded]) {
718+
if (text.length > 0 && ![_textInputbar limitExceeded]) {
717719
return YES;
718720
}
719721

@@ -738,14 +740,14 @@ - (void)didPressRightButton:(id)sender
738740

739741
- (void)editText:(NSString *)text
740742
{
741-
if (![self.textInputbar canEditText:text]) {
743+
if (![_textInputbar canEditText:text]) {
742744
return;
743745
}
744746

745747
// Caches the current text, in case the user cancels the edition
746748
[self slk_cacheTextToDisk:self.textView.text];
747749

748-
[self.textInputbar beginTextEditing];
750+
[_textInputbar beginTextEditing];
749751

750752
// Setting the text after calling -beginTextEditing is safer
751753
[self.textView setText:text];
@@ -758,23 +760,23 @@ - (void)editText:(NSString *)text
758760

759761
- (void)didCommitTextEditing:(id)sender
760762
{
761-
if (!self.textInputbar.isEditing) {
763+
if (!_textInputbar.isEditing) {
762764
return;
763765
}
764766

765-
[self.textInputbar endTextEdition];
767+
[_textInputbar endTextEdition];
766768

767769
// Clears the text and but not the undo manager
768770
[self.textView slk_clearText:NO];
769771
}
770772

771773
- (void)didCancelTextEditing:(id)sender
772774
{
773-
if (!self.textInputbar.isEditing) {
775+
if (!_textInputbar.isEditing) {
774776
return;
775777
}
776778

777-
[self.textInputbar endTextEdition];
779+
[_textInputbar endTextEdition];
778780

779781
// Clears the text and but not the undo manager
780782
[self.textView slk_clearText:NO];
@@ -786,7 +788,7 @@ - (void)didCancelTextEditing:(id)sender
786788
- (BOOL)canShowTypingIndicator
787789
{
788790
// Don't show if the text is being edited or auto-completed.
789-
if (self.textInputbar.isEditing || self.isAutoCompleting) {
791+
if (_textInputbar.isEditing || self.isAutoCompleting) {
790792
return NO;
791793
}
792794

@@ -916,7 +918,7 @@ - (void)slk_handlePanGestureRecognizer:(UIPanGestureRecognizer *)gesture
916918
static BOOL dragging = NO;
917919
static BOOL presenting = NO;
918920

919-
__block UIView *keyboardView = [self.textInputbar.inputAccessoryView keyboardViewProxy];
921+
__block UIView *keyboardView = [_textInputbar.inputAccessoryView keyboardViewProxy];
920922

921923
// When no keyboard view has been detecting, let's skip any handling.
922924
if (!keyboardView) {
@@ -947,7 +949,7 @@ - (void)slk_handlePanGestureRecognizer:(UIPanGestureRecognizer *)gesture
947949

948950
presenting = YES;
949951
#else
950-
if ([gesture.view isEqual:self.textInputbar] && gestureVelocity.y < 0) {
952+
if ([gesture.view isEqual:_textInputbar] && gestureVelocity.y < 0) {
951953
[self presentKeyboard:YES];
952954
}
953955
return;
@@ -965,7 +967,7 @@ - (void)slk_handlePanGestureRecognizer:(UIPanGestureRecognizer *)gesture
965967
[self presentKeyboard:NO];
966968

967969
// So we can capture the keyboard's view
968-
keyboardView = [self.textInputbar.inputAccessoryView keyboardViewProxy];
970+
keyboardView = [_textInputbar.inputAccessoryView keyboardViewProxy];
969971

970972
originalFrame = keyboardView.frame;
971973
originalFrame.origin.y = CGRectGetMaxY(self.view.frame);
@@ -979,7 +981,7 @@ - (void)slk_handlePanGestureRecognizer:(UIPanGestureRecognizer *)gesture
979981
}
980982
case UIGestureRecognizerStateChanged: {
981983

982-
if (CGRectContainsPoint(self.textInputbar.frame, gestureLocation) || dragging || presenting){
984+
if (CGRectContainsPoint(_textInputbar.frame, gestureLocation) || dragging || presenting){
983985

984986
if (CGPointEqualToPoint(startPoint, CGPointZero)) {
985987
startPoint = gestureLocation;
@@ -1271,7 +1273,7 @@ - (void)slk_prepareForInterfaceTransitionWithDuration:(NSTimeInterval)duration
12711273

12721274
- (void)didPressReturnKey:(id)sender
12731275
{
1274-
if (self.textInputbar.isEditing) {
1276+
if (_textInputbar.isEditing) {
12751277
[self didCommitTextEditing:sender];
12761278
}
12771279
else {
@@ -1284,7 +1286,7 @@ - (void)didPressEscapeKey:(id)sender
12841286
if (self.isAutoCompleting) {
12851287
[self cancelAutoCompletion];
12861288
}
1287-
else if (self.textInputbar.isEditing) {
1289+
else if (_textInputbar.isEditing) {
12881290
[self didCancelTextEditing:sender];
12891291
}
12901292

@@ -1611,7 +1613,7 @@ - (void)showAutoCompletionView:(BOOL)show
16111613
{
16121614
// Reloads the tableview before showing/hiding
16131615
if (show) {
1614-
[self.autoCompletionView reloadData];
1616+
[_autoCompletionView reloadData];
16151617
}
16161618

16171619
self.autoCompleting = show;
@@ -1762,7 +1764,7 @@ - (void)slk_invalidateAutoCompletion
17621764
_foundWord = nil;
17631765
_foundPrefixRange = NSMakeRange(0, 0);
17641766

1765-
[self.autoCompletionView setContentOffset:CGPointZero];
1767+
[_autoCompletionView setContentOffset:CGPointZero];
17661768
}
17671769

17681770
- (void)slk_hideAutoCompletionViewIfNeeded
@@ -2102,7 +2104,7 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
21022104

21032105
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
21042106
{
2105-
if ([scrollView isEqual:self.autoCompletionView]) {
2107+
if ([scrollView isEqual:_autoCompletionView]) {
21062108
CGRect frame = self.autoCompletionHairline.frame;
21072109
frame.origin.y = scrollView.contentOffset.y;
21082110
self.autoCompletionHairline.frame = frame;
@@ -2179,7 +2181,7 @@ - (void)slk_updateViewConstraints
21792181
self.scrollViewHC.constant = [self slk_appropriateScrollViewHeight];
21802182
self.keyboardHC.constant = [self slk_appropriateKeyboardHeightFromRect:CGRectNull];
21812183

2182-
if (self.textInputbar.isEditing) {
2184+
if (_textInputbar.isEditing) {
21832185
self.textInputbarHC.constant += self.textInputbar.editorContentViewHeight;
21842186
}
21852187

@@ -2208,8 +2210,8 @@ - (UIKeyCommand *)slk_returnKeyCommand
22082210
#ifdef __IPHONE_9_0
22092211
if ([UIKeyCommand respondsToSelector:@selector(keyCommandWithInput:modifierFlags:action:discoverabilityTitle:)] ) {
22102212
// Only available since iOS 9
2211-
if (self.textInputbar.isEditing) {
2212-
command.discoverabilityTitle = [self.textInputbar.editorRightButton titleForState:UIControlStateNormal] ? : NSLocalizedString(@"Commit Editing", nil);
2213+
if (_textInputbar.isEditing) {
2214+
command.discoverabilityTitle = [_textInputbar.editorRightButton titleForState:UIControlStateNormal] ? : NSLocalizedString(@"Commit Editing", nil);
22132215
}
22142216
else if (self.textView.text.length > 0) {
22152217
command.discoverabilityTitle = [self.rightButton titleForState:UIControlStateNormal] ? : NSLocalizedString(@"Send", nil);
@@ -2230,8 +2232,8 @@ - (UIKeyCommand *)slk_escKeyCommand
22302232
if (self.isAutoCompleting) {
22312233
command.discoverabilityTitle = NSLocalizedString(@"Exit Auto-Completion", nil);
22322234
}
2233-
else if (self.textInputbar.isEditing) {
2234-
command.discoverabilityTitle = [self.textInputbar.editorRightButton titleForState:UIControlStateNormal] ? : NSLocalizedString(@"Exit Editing", nil);
2235+
else if (_textInputbar.isEditing) {
2236+
command.discoverabilityTitle = [_textInputbar.editorRightButton titleForState:UIControlStateNormal] ? : NSLocalizedString(@"Exit Editing", nil);
22352237
}
22362238
else if (!self.isExternalKeyboardDetected && self.keyboardHC.constant != 0) {
22372239
command.discoverabilityTitle = NSLocalizedString(@"Hide Keyboard", nil);
@@ -2375,6 +2377,8 @@ - (void)didReceiveMemoryWarning
23752377

23762378
- (void)dealloc
23772379
{
2380+
[self slk_unregisterNotifications];
2381+
23782382
_tableView.delegate = nil;
23792383
_tableView.dataSource = nil;
23802384
_tableView = nil;
@@ -2389,7 +2393,6 @@ - (void)dealloc
23892393
_autoCompletionView.dataSource = nil;
23902394
_autoCompletionView = nil;
23912395

2392-
_textInputbar.textView.delegate = nil;
23932396
_textInputbar = nil;
23942397
_textViewClass = nil;
23952398

@@ -2407,8 +2410,6 @@ - (void)dealloc
24072410
_typingIndicatorViewHC = nil;
24082411
_autoCompletionViewHC = nil;
24092412
_keyboardHC = nil;
2410-
2411-
[self slk_unregisterNotifications];
24122413
}
24132414

24142415
@end

0 commit comments

Comments
 (0)