Obj-c - Disable uibutton if textView is empty? - ios

I'm trying to disable my uibutton if a UITextView is empty with the below code. The button disables if the field is empty, however, when I use the below, the button doesn't seem to "enable" if the text view has values in it? Any idea why this is?
Edit/Update: I've updated my code below (problem half solved). In this scenario, my send button is disabled if the text view is empty. However, once I have text in my view and I press send, if I hit send a second time (and the text view is once again empty), send button is no longer disabled. Why is this?
- (void)viewDidLoad {
[super viewDidLoad];
self.sendButton.userInteractionEnabled = NO;
}
- (void)textViewDidChange:(UITextView *)textView {
self.sendButton.userInteractionEnabled = (textView.text.length > 0);
}

You have to implement UITextView delegate:
- (void)textViewDidChange:(UITextView *)textView {
sendButton.userInteractionEnabled = (textView.text.length > 0);
}

you must be setting text in self.replyField after viewDidLoad is executed.
try executing below code whenever you change the value in self.replyField:
if (self.replyField.text.length > 0) {
sendButton.userInteractionEnabled = YES;
}
else {
sendButton.userInteractionEnabled = NO;
}
you can use isEnabled instead of userInteractionEnabled

you should implement below delegate method for enabling and disabling button based on text values
optional func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
}

Related

Setting UITextField not editable or editable

I want to know how to disable UITextField, i.e I placed a UIButton in the frame of UITextField for design purpose.
When I tap my button in UITextField the keyboard appears, but I don't want the keyboard to be displayed!
Here is my code so far:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return textField !=textfiled1;
return textField !=textfiled2;
}
You can enable or disable user interaction on the textfield using this property:
textField.userInteractionEnabled = NO;
In Swift 5
textField.isUserInteractionEnabled = false
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
You can use this too
Accepted answer in Swift 4:
textField.isUserInteractionEnabled = false
I think your UIButton is not on UITextField. so bringToFromView first then your UIButton selector method called first. Because compiler check Event in hierarchy. if first control response the event then it wan't looking for next.
I have tested your scenario and its working fine at my end.
swift version of my Obj-C Answer
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
Use the below property.
textField.userInteractionEnabled = NO;
you can achieve this with the help of:
If you want to disable editing you can do this
textField.editable = NO;
and if you want to make it editable again you can do this
textField.editable = YES;

Disable editing only for UITextField?

I tried the following ways:
properties enabled and "user interaction enabled";
method textFieldShouldBeginEditing:.
But they disable all the possible actions with text field. I need to disable the editing only but allow users to copy text value. How to implement this?
Implement and set the following UITextFieldDelegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return NO;
}
This makes the field not editable while still retaining that select/copy functionality. The editing property is readonly and unrelated.
This might be helpful for you. Try
YourTextField.editing = NO;
Keep the enabled property on, and the user interaction on. Otherwise you won't be able to select the text.
set the delegate of the UITextField to the ViewController and implement :
Swift:
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
return false
}
In addition to tasuhka's response, make sure
YourTextField.selectable = YES;

How to make a UITextField selectable but not editable?

I hope that user can copy and paste the text but not edit them. I use a delegate UITextField method to implement this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO;
}
In this way although the text is selectable and not editable, but when you select the text, the keyboard always shows up, which is a little bit annoying cause you can not edit the text. So is there anyway to make text selectable and not editable, without showing the keyboard?
What you need is to allow the control to receive all user interaction events. So, do not return NO from textFieldShouldBeginEditing. Instead, do the following:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return textField != _yourReadOnlyTextField;
}
This will allow the user to select the text and also to select options like Cut, Copy and Define from the popup menu.
UPDATE:
Also, for completeness, you may want to prevent the keyboard from showing up at all on readyonly textfields. So based on the accepted answer to this question: uitextfield hide keyboard?, you may want to add:
- (void)viewDidLoad
{
// Prevent keyboard from showing up when editing read-only text field
_yourReadOnlyTextField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
}
Update for Swift users:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return textField != self.yourReadOnlyTextField;
}
and when the view loads
override func viewDidLoad() {
super.viewDidLoad()
self.selfCodeEdit.inputView = UIView.init();
}
You should implement another UITextField's delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
//UPDATE Also, there is a questions like this here How to disable UITextField's edit property?.
if you have more then one textFields you can do in this way
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == firstTextField || textField == secondTextField {
return false
}
return true
}
You can do that directly through the storyboard.
Select your UITextView then in your Attributes inspector.
You have some behavior params :

Disable Keyboard for UITextfield

I'm wondering how to disable the inputview of a UITextfield. Setting textField.inputView = nil; or [textField setInputView:nil] in ShouldBeginEditing doesn't do anything, and using the userInteraction property removes the ability to interact with the field. Ideally, I'd like to remove both the cursor and the keyboard while still being able interact with and switch between textfield methods, using ShouldBeginEditing and ShouldEndEditing. Is there any way to accomplish this?
You should do this:
myTextField.inputView = UIView.new; //Empty UIView
Setting it to nil just means the default keyboard is used.
To get rid of the caret, subclass the UITextField and override caretRectForPosition:
- (CGRect) caretRectForPosition:(UITextPosition*)position
{
return CGRectZero;
}
Try this :
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hide both keyboard and blinking cursor.
}
or
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder]; // hides keyboard
}

How to know when UITextView became first responder

How to handle when uitextview became first responder. I have text in text view and I want when the view became active want to clear the text. How can I do that? Thanks in advance
You can definitely use a UITextViewDelegate method of:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
Just return YES and intercept inside that method. You can also do it for UITextFields with UITextFieldDelegate and:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
textViewShouldBeginEditing actually triggers before the text view becomes the first responder.
textViewDidBeginEditing will trigger once the text view becomes the first responder and would be the best place to execute code that needs to know what the active textview is.
If you are not really worried about which field is active and just want to clear the text once the field is tapped on you could use either function.
EDIT: The same methods are available for text fields.
As above, you can override becomeFirstResponder but note that you must call the superclass implementation. If you don't, things like popping the keyboard on a text field won't work. i.e.
override func becomeFirstResponder() -> Bool {
if super.becomeFirstResponder() {
// set up the control state
return true
}
return false
}
The Swift 4 solution
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}
Previous answers do the job great for UITextBox, but if you have a custom class derived from NSResponder and need to know when it becomes first responder:
-(BOOL) becomeFirstResponder
{
// Your stuff here
return YES;
}

Resources