Swift 2.1 - Inputing information into a TextView which populates a Textfield at the same time? - ios

I am currently using a framework called "Swift Validator", it only allows for textfields to be used. In my case I need a textView, my question is how do I input information that I type into the textview which then populates a textfield in the background at the same time? as well as when removing inputed text?

Use the UITextViewDelegate method textViewDidChange to listen for changes to the Text View. When the method is called, update the Text Field. Here's an example:
func textViewDidChange(theTextView: UITextView) {
self.textField.text = theTextView.text
}
Don't forget to extend UITextViewDelegate and to set the Text View's delegate to self.

Related

iOS - Dismiss keyboard on Return key [duplicate]

This question already has answers here:
dismiss keyboard with a uiTextView
(6 answers)
Closed 7 years ago.
I am working with a UITextView and I need to dismiss the keyboard by pressing return key.
This is what I'm doing:
class MyController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var writeNote: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.writeNote.delegate = self
}
how to proceed? When using a UITextField I would simply call the delegate for textFieldShouldReturn method but I don't appear to find anything like that for a textView. I can not use a textField because I need a larger amount of rows. The TextView tooltip says "When a user taps a text view, a keyboard appears; when a user taps Return in the keyboard, the keyboard disappears and..." but it doesn't do it by default..
UITextView doesn't have any such direct delegate method to hide the keyboard. You have to work around with the shouldChangeTextInRange: replacementTextmethod.
In this method, you can check for a new line character and if you get one, you can resign.

Is there a delegate call for when the text is changed on a UITextView?

When I set my UITextView programmatically like this:
[self.textView setText:#""];
The delegate method textViewDidChange: does not get called. Is there a way I can find that without making a UITextView subclass?
When manually setting the text of a UITextView with code, the textViewDidChange: method does not get called. (If you have your text view's delegate set, it will get called when the user edits it, though.)
One possible workaround would be to manually call textViewDidChange: anytime you edit the text. For example:
[self.textView setText:#""];
[self textViewDidChange:self.textView];
Kind of a hackish way of doing it, but it gets the job done.
I upvoted #rebello95's response because it is one approach. But another, less hacky approach is to do as
- (void)whereIManuallyChangeTextView
{//you don't actually have to create this method. It's simply wherever you are setting the textview to empty
[self.textView setText:#""];
[self respondToChangeInTextView:self.textView];
}
- (void)textViewDidChange:(UITextView *)textView
{
//...some work and then
[self respondToChangeInTextView:textView];
}
- (void)respondToChangeInTextView:(UITextView *)textView
{
//what you want to happen when you programmatically/manually or interactively change the textview
}
This snippet exemplifies a respectable pattern that will make your code more readable.
In swift you could override the text variable from the UITextView class:
class MyTextView: UITextView {
override public var text: String? {
didSet {
self.textViewDidChange(self)
}
}
}
Old post, but I had the same problem and thought I would share my solution (in Swift).
textViewDidChange(_ textView: UITextView) does not get called by just setting the text property, but it does get called when using replace(range: UITextRange, withText: String). So you need to create a UITextRange for the entire string of the UITextView and replace it with a new string.
// Create a range of entire string
let textRange = textView.textRange(from: textView.beginningOfDocument, to: textView.endOfDocument)
// Create a new string
let newText = ""
// Call Replace the string in your textView with the new string
textView.replace(textRange!, withText: newText)
That should do it. Of course, you need to set up UITextViewDelegate for this to work:
class ViewController: UIViewController, UITextViewDelegate {
You could also subclass UITextView and override setText to include
[self textViewDidChange:self.textView]
so that you don't have to call it every time you set the text of your UITextView.
delegate method textDidChange does not respond to programmatical changes in textes, you can use observation to get notified
declare your text view variable with #objc dynamic
declare and hold a variable with type NSKeyValueObservation
use function observe(_:changeHandler:) bind your text view's text property, hold the return value with variable declared in step 2
observe changes in changeHandler
example:
#objc dynamic private var textView: UITextView!
private var observation: NSKeyValueObservation?
func bind() {
observation = observe(\.textView.text, options: [.old, .new]) { object, change in
print(object, change)
}
}
use this instead: (this won't reset the current text)
[self.textView insertText:#"something"];
this will call the delegate and will add text where the cursor is. Of course if you want to reset the whole text you can either:
[self.textView setText:#""];
[self textViewDidChange:self.textView];
or
[self.textView setText:#""];
[self.textView insertText:#"something"];

if UITextField clicked

I am doing an application and I need to detect when a UITextField is clicked. I tried using touchesBegan but it doesn't react to textfield when clicked, only outside of it. I am only starting with Objective-C so if you give me advice, please let it be detailed. Thank you.
You can try the delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// Do something
}
As long as the text field has interaction enabled and is editable. However, a second tap will not be detected if the text field is already the first responder.
Set the textview's delegate property to an object that overloads the textViewShouldBeginEditing: function. You could also use the textFieldDidBeginEditing: function of the same delegate.

How To Stop keyBoard Appearence at click event on UITextview?

In my IPad Application i am using TextView only for Text Displaying.As i need to display a Larger Text Thats Why i am using UITextview due to its Scrolling Property instead of using UILabel.
In my application i do not need to edit Text in UITextview ,but problem for me is that when i click on Textview for scrolling the keyboard appear its hide my textview so i want that my keyboard is never appear on click event.i make a search but not find any Suitable solution.Any help will be appriated.Thanx
NEW ANSWER (previous one was not working properly)
OK so since that is not working because it disables scrolling also, you should try to:
Implement UITextFieldDelegate protocol
In your view controller add the text
#interface YourViewController () <UITextViewDelegate>
In viewDidLoad set yourself as a delegate:
yourUITextView.delegate = self;
Implement the delegate method below:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return NO;
}
When the textview is about to edit the text, this method will be called automatically. It returns no, so the editing won't start.
It is very important that you undo the changes from the previous answers: Do not set the editable field to NO
I tried it and it's working. Hope it helps!
OLD ANSWER
when you declare the variable, or in your viewdidload method, set the editable property to NO:
yourUITextView.editable = NO;
or
[yourUITextView setEditable:NO]
That should prevent the keyboard from appearing.
Go to .XIB file and you can uncheck behavior editable or programmatically
textView.editable = NO;

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