How to tell UITextView is dirty? - ios

Currently, I test whether a UITextView is dirty or not by:
if ([self.textView.undoManager canUndo]) ...
Is this always correct? Or is there any isDirty-like method in UITextView?

You can listen to lots of things with the UITextViewDelegate
Check out the UITextViewDelegate documentation for comprehensive details
- (void)textViewDidBeginEditing:(UITextView *)textView
- (void)textViewDidEndEditing:(UITextView *)textView
- (void)textViewDidChange:(UITextView *)textView
There's more to do with shouldBegin and shouldEnd editing, and also selection changes.
Hope that helps out.

Related

textFieldShouldReturn method of UITextview

Needing to present user a multiline text field for comment entry, I am using a UITextView instead of a UITextField. I would like to use textFieldShouldReturn on my UITextView to send the data to server. How might I do that? Based on my readings so far, the method is only applicable to UITextField. So what is the equivalent for UITextView?
By adding the UITextViewDelegate to your viewControllerHeader
#interface ViewController : UIViewController<UITextViewDelegate>
This'll allow you access to the UITextViewDelegate methods of which there are a couple of which should allow you to know when the user has either pressed return or let you know when they have finished editing, for example
- (void)textViewDidEndEditing:(UITextView *)textView{
//From here you can action your methods to send the data to your server as required etc.
}
There's also this method
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//Which you can use to listen for the #"\" return action if you prefer.
}
I hope this helps

UITextView insertText call shouldChangeTextInRange: replacementText:

I have a UITextView. I have the delegate for myTextView set to self and, when I do normal editing, this method calls just fine:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(#"Called");
}
In my app, I call in my code: [myTextView insertText:#"Hello World"];. When I do, I need to call textView:shouldChangeTextInRange:replacementText: after the text is inserted. How do I do this?
Call it explicitly. Call it before editing and only perform the edit if it returns YES. To call it explicitly you need to know the selected range (get the selected range with selectedTextRange), that's it. You already have the text to add and the text view.
Thanks for the answer, #Wain!
Here's what worked:
- (void)insertText
{
NSString *stringToAdd = #"Hello World";
NSString *replacementText = [myTextView.text stringByAppendingString:stringToAdd];
[napkinTextView insertText:stringToAdd];
[self textView:myTextView shouldChangeTextInRange:NSMakeRange(0, stringToAdd.length) replacementText:replacementText];
}

is shouldChangeTextInRange is compulsory in UITextViewDelegate?

I have implemented UITextViewDelegate in my ViewController,
After setting delegate to my TextView as
self.addressTextView.delegate=self;
Now i can only set the text as,
[self.addressTextView setText:#"Tamil Nadu, India"];
I am unable to edit the text using keybord. After Implementing shouldChangeTextInRange method only i am able to edit the content in UITextView.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
I dont know why?? is shouldChangeTextInRange compulsory if we implementing UITextViewDelegate
NO. Its not compulsary. You can set text like that
textView.text = #"Hello";
This method will be call when you are try to write somthing in your text view.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Its not compulsory in the protocol definition, so more than probably something else is going on. From reading that implementing the delegate method mentioned fixes this, its quite possible that your controller's superclass implements the method and only allows the text you've got above. If that's the case, the workaround you've implemented is in fact the easiest way to fix this.
For your information, everything that must be implemented when you declare that a class follows a protocol will be indicated by the compiler : you will get an error when you build if a method or a property is missing.

UITextView Change URL

I have a UITextView that has a text of say www.foo.com
when the user clicks on it I want to visit www.foo.com/bar/cow/index.html instead of what appears in the text property of the UITextView
What is the easiest way to do this?
Have your class conform to the UITextViewDelegate protocol. Then implement - (void)textViewDidBeginEditing:(UITextView *)textView in a similar way:
- (void)textViewDidBeginEditing:(UITextView *)textView{
NSString *url = [NSString stringWithFormat:#"%#<additionalTextHere>", textView.text];
//do whetever with url
}

ipad: how to know editing done in UITextView?

I've a textview. I want to know whether the changes done in textview while pressing back button in UINavigationBackbutton. how to compare old and new text entered in UItextview?
If there is any changes, i'll ask Do u want to save the changes?.
Just set a flag when your UITextView did begin editing (wasEdited=YES) and save the current state of the text (originalText = myTextView.text) then on backbutton check the (originalText isEqualToString:myTextView.text && wasEdited)
The was edited tag is to avoid string comparaison in case the user didn't get into editing :)
iphony,
Better late than never...
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSLog(#"textViewShouldBeginEditing");
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"textViewDidBeginEditing");
}
- (void)textViewDidChange:(UITextView *)textView {
NSLog(#"textViewDidChange");
}
- (void)textViewDidChangeSelection:(UITextView *)textView {
NSLog(#"textViewDidChangeSelection");
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
NSLog(#"textViewShouldEndEditing");
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
NSLog(#"textViewDidEndEditing");
}

Resources