Skip to content

Commit

Permalink
[iOS] Resubmit facebook#24714 and fixes kvo crash
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongwuzw committed May 14, 2019
1 parent de0d7cf commit fc3a1f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ NS_ASSUME_NONNULL_BEGIN

- (void)skipNextTextInputDidChangeSelectionEventWithTextRange:(UITextRange *)textRange;
- (void)selectedTextRangeWasSet;
- (void)cleanupWithTextField:(UITextField<RCTBackedTextInputViewProtocol> *)backedTextInputView;

@end

Expand Down
18 changes: 15 additions & 3 deletions Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@ - (instancetype)initWithTextField:(UITextField<RCTBackedTextInputViewProtocol> *

[_backedTextInputView addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
[_backedTextInputView addTarget:self action:@selector(textFieldDidEndEditingOnExit) forControlEvents:UIControlEventEditingDidEndOnExit];
[_backedTextInputView addObserver:self forKeyPath:@"selectedTextRange" options:NSKeyValueObservingOptionNew context:NULL];
}

return self;
}

- (void)dealloc
- (void)cleanupWithTextField:(UITextField<RCTBackedTextInputViewProtocol> *)backedTextInputView
{
[_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingChanged];
[_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit];
[backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingChanged];
[backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit];
[backedTextInputView removeObserver:self forKeyPath:@"selectedTextRange"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// UITextField doesn't have a delegate like UITextView to get notified on selection. Use KVO to observe changes.
if ([keyPath isEqualToString:@"selectedTextRange"]) {
[self textFieldProbablyDidChangeSelection];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

#pragma mark - UITextFieldDelegate
Expand Down
1 change: 1 addition & 0 deletions Libraries/Text/TextInput/Singleline/RCTUITextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ - (instancetype)initWithFrame:(CGRect)frame

- (void)dealloc
{
[_textInputDelegateAdapter cleanupWithTextField:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Expand Down

0 comments on commit fc3a1f8

Please sign in to comment.