-
Notifications
You must be signed in to change notification settings - Fork 494
UITextField return command #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AndrewMcDrew
wants to merge
3
commits into
ReactiveCocoa:master
Choose a base branch
from
iqmessenger-bv:textFieldReturnCommand
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// UITextField+RACCommandSupport.h | ||
// ReactiveObjC | ||
// | ||
// Created by Andrew Urban on 9/12/17. | ||
// Copyright © 2017 GitHub. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@class RACCommand<__contravariant InputType, __covariant ValueType>; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface UITextField (RACCommandSupport) | ||
|
||
/// Sets the command for the text field's return key. When the return key is clicked, the command is | ||
/// executed with the sender of the event. The text field's enabledness is bound | ||
/// to the command's `canExecute`. | ||
@property (nonatomic, strong, nullable) RACCommand<__kindof UITextField *, id> *rac_returnCommand; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// UITextField+RACCommandSupport.m | ||
// ReactiveObjC | ||
// | ||
// Created by Andrew Urban on 9/12/17. | ||
// Copyright © 2017 GitHub. All rights reserved. | ||
// | ||
|
||
#import "UITextField+RACCommandSupport.h" | ||
#import <ReactiveObjC/EXTKeyPathCoding.h> | ||
#import "RACCommand.h" | ||
#import "RACDisposable.h" | ||
#import "RACSignal+Operations.h" | ||
#import <objc/runtime.h> | ||
|
||
static void *UITextFieldRACReturnCommandKey = &UITextFieldRACReturnCommandKey; | ||
static void *UITextFieldEnabledDisposableKey = &UITextFieldEnabledDisposableKey; | ||
|
||
@implementation UITextField (RACCommandSupport) | ||
|
||
- (RACCommand *)rac_returnCommand { | ||
return objc_getAssociatedObject(self, UITextFieldRACReturnCommandKey); | ||
} | ||
|
||
- (void)setRac_returnCommand:(RACCommand *)command { | ||
objc_setAssociatedObject(self, UITextFieldRACReturnCommandKey, command, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
|
||
// Check for stored signal in order to remove it and add a new one | ||
RACDisposable *disposable = objc_getAssociatedObject(self, UITextFieldEnabledDisposableKey); | ||
[disposable dispose]; | ||
|
||
if (command == nil) return; | ||
|
||
disposable = [command.enabled setKeyPath:@keypath(self.enabled) onObject:self]; | ||
objc_setAssociatedObject(self, UITextFieldEnabledDisposableKey, disposable, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
|
||
[self rac_hijackActionAndTargetIfNeeded]; | ||
} | ||
|
||
- (void)rac_hijackActionAndTargetIfNeeded { | ||
SEL hijackSelector = @selector(rac_commandPerformAction:); | ||
|
||
for (NSString *selector in [self actionsForTarget:self forControlEvent:UIControlEventEditingDidEndOnExit]) { | ||
if (hijackSelector == NSSelectorFromString(selector)) { | ||
return; | ||
} | ||
} | ||
|
||
[self addTarget:self action:hijackSelector forControlEvents:UIControlEventEditingDidEndOnExit]; | ||
} | ||
|
||
- (void)rac_commandPerformAction:(id)sender { | ||
[self.rac_returnCommand execute:sender]; | ||
} | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// RACTestUITextField.h | ||
// ReactiveObjC | ||
// | ||
// Created by Andrew Urban on 9/12/17. | ||
// Copyright © 2017 GitHub. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
// Enables use of -sendActionsForControlEvents: in unit tests. | ||
@interface RACTestUITextField : UITextField | ||
|
||
+ (instancetype)textField; | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// RACTestUITextField.m | ||
// ReactiveObjC | ||
// | ||
// Created by Andrew Urban on 9/12/17. | ||
// Copyright © 2017 GitHub. All rights reserved. | ||
// | ||
|
||
#import "RACTestUITextField.h" | ||
|
||
@implementation RACTestUITextField | ||
|
||
+ (instancetype)textField { | ||
RACTestUITextField *textField = [self new]; | ||
return textField; | ||
} | ||
|
||
// Required for unit testing – controls don't work normally | ||
// outside of normal apps. | ||
-(void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | ||
[target performSelector:action withObject:self]; | ||
#pragma clang diagnostic pop | ||
} | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// UITextFieldRACSupportSpec.m | ||
// ReactiveObjC | ||
// | ||
// Created by Andrew Urban on 9/12/17. | ||
// Copyright © 2017 GitHub. All rights reserved. | ||
// | ||
|
||
@import Quick; | ||
@import Nimble; | ||
|
||
#import "RACControlCommandExamples.h" | ||
#import "RACTestUITextField.h" | ||
|
||
#import "UITextField+RACCommandSupport.h" | ||
#import "RACCommand.h" | ||
#import "RACDisposable.h" | ||
|
||
QuickSpecBegin(UITextFieldRACSupportSpec) | ||
|
||
qck_describe(@"UITextField", ^{ | ||
__block UITextField *textField; | ||
|
||
qck_beforeEach(^{ | ||
textField = [RACTestUITextField textField]; | ||
expect(textField).notTo(beNil()); | ||
}); | ||
|
||
qck_itBehavesLike(RACControlCommandExamples, ^{ | ||
return @{ | ||
RACControlCommandExampleControl: textField, | ||
RACControlCommandExampleActivateBlock: ^(UITextField *textField) { | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | ||
[textField sendActionsForControlEvents:UIControlEventEditingDidEndOnExit]; | ||
#pragma clang diagnostic pop | ||
} | ||
}; | ||
}); | ||
}); | ||
|
||
QuickSpecEnd |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs a
-asScopedDisposable
in order to not leak when theUITextField
is deallocated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither
UIBarButtonItem (RACCommandSupport)
norUIButton (RACCommandSupport)
make use ofRACScopedDisposable
in the same case. Do you think all of them should be revised to address this problem?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think so, but I'm honestly not sure. I'm not very familiar with this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too) So I should investigate more into this possible memory leak but can't do it right now. Will try to look at this soon.