Detect Start and Stop Editing UITextView
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate
Here you can find several useful methods to investigate:
textViewDidBeginEditing:
textViewDidEndEditing:
Moreover to leave UITextView
you often should implement action that calls [yourTextView resignFirstResponder];
Objective-C example
//you may specify UITextViewDelegate protocol in .h file interface, but it's better not to expose it if not necessary
@interface ExampleViewController()<UITextViewDelegate>
@end
@implementation ExampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
//assuming _textView is already instantiated and added to its superview
_textView.delegate = self;
}
//it's nice to separate delegate methods with pragmas but it's up to your local code style policy
#pragma mark UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
//handle user taps text view to type text
}
- (void)textViewDidEndEditing:(UITextView *)textView {
//handle text editing finished
}
@end
Swift Example
class TextViewEventsViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var exampleTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.exampleTextView.delegate = self
}
func textViewDidBeginEditing(_ textView: UITextView) {
print("exampleTextView: BEGIN EDIT")
}
func textViewDidEndEditing(_ textView: UITextView) {
print("exampleTextView: END EDIT")
}
}
Detect when a UITextView has been edited
To do this, you need to conform to the UITextViewDelegate
protocol and then set the textView's delegate to self.
So, after UIViewController
on the beginning of this view controller, you need to add the protocol conformance. Example
class MyViewController: UIViewController, UITextViewDelegate {
then set the delegate in viewDidLoad()
:
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
Now, you're set up for success. All of the delegate methods on UITextViewDelegate are optional, so you only need to implement the ones you want. In this case, the one you want to implement is func textViewDidChange(_ textView: UITextView)
That method will get triggered every single time the text in the text field updates.
how to detect which textview is being edited swift
As long as you have properties that refer to the two text views you can simply see which one was passed to your delegate and act accordingly:
func textViewDidChange(textView: UITextView) { //Handle the text changes here
guard let currentUser = PFUser.currentUser() else {
return
}
if (textView == self.bioTextView){
currentUser["bio"] = textView.text
currentUser.saveInBackground()
} else {
currentUser["displayName"] = textView.text
currentUser.saveInBackground()
}
}
textViewDidBeginEditing on 2 separate textViews in swift
you need to change private func textViewDidBeginEditing2(_ definitionTextView: UITextView)
to func textViewDidBeginEditing(_ definitionTextView: UITextView)
How do I check when a UITextField changes?
SWIFT
Swift 4.2
textfield.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
and
@objc func textFieldDidChange(_ textField: UITextField) {
}
SWIFT 3 & swift 4.1
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
and
func textFieldDidChange(_ textField: UITextField) {
}
SWIFT 2.2
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
and
func textFieldDidChange(textField: UITextField) {
//your code
}
OBJECTIVE-C
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
and textFieldDidChange method is
-(void)textFieldDidChange :(UITextField *) textField{
//your code
}
How to detect when a TextView is selected but before it becomes first responder?
You can get your first responder reference before system shows the keyboard using UITextViewDelegate.textViewShouldBeginEditing(_:)
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
var currentFirstResponder: UITextView?
// iOS will call this before it shows keyboard
// If you return true, it will show keyboard
// If you return false, it will not show keyboard
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
currentFirstResponder = textView
return true
}
}
Related Topics
How to Replace the Values of Labels in iOS-Charts
Swift Build' on Terminal Throw 'Error: Root Manifest Not Found'
Swift Any Difference Between Closures and First-Class Functions
How to Get Part of a String in Swift
How Would I Create a Constant That Could Be One of Several Strings Depending on Conditions
Nonfailable Enum Initializer with Default Value
Navigationview Bar Material Invisible on iOS 15
Swiftui Classes That Conforms Observableobject Should Be Singleton
Are Built-In Intrinsic Functions Available in Swift 3
Appdelegate Segue Alternative Pass Data
Integrate Existing Aws Cognito User Pool into iOS Project with Amplify
How to Get Mouse Location with Swiftui
Why Can't I Use Self in a Func Swift
How to Customize the Title/Subtitle Font in Callout from Mkannotationview or Just Hide Them