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");
}
Related
I'm trying to hide the keyboard after the user clicked on the return button of the keyboard.
I'm using this function to hide it:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
When the textview is empty this function works but once there is a characters in the textview,nothing happens and the keyboard doesnt get hidden.
I would suggest to use [self endEditing:YES]; or self.view endEditing:YES]
Found the answer. Was my fault. I had a function which once the user finished editing i'm checking if it's empty or not. For some reason returned no instead of yes. Thanks for the help
I want to be able to send a message as soon as the user touches return on the on-screen keyboard. I have a send UIButton, how do I make it listen for the return key?
Thanks!
Your button Method:
- (void) btnPressed:(id)sender {
{
Add Delegate Stuff if you haven't
#interface YourViewController : UIViewController <UITextFieldDelegate>
And when you call your textField:
yourTextField.delegate = self;
Then, you should just call the button's action method in the delegate shouldReturn method:
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[self btnPressed:nil];
// whatever else you need
return YES;
}
Hook textFieldShouldReturn: and send a message to the button or directly to the function that button calls.
If you're using UITextView, it takes a bit of trickery:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (textView == messageInput) {
if ([text isEqualToString:#"\n"]) {
// your code goes here
return NO;
}
}
return YES;
}
Is there a way to disable the iOS keyboard's feature where when you hold backspace down for long enough it starts to delete many characters at a time?
You can use the UITextViewDelegate (or UITextFieldDelegate method) shouldChangeTextInRange stop the user from deleting words at a time.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (text.length == 0) {
//Backspace
return range.length == 1;
}
return YES;
}
I have two UITextViews! The problem i have is that I want an action to be performed when one of the textViews is entered and not the other! I placed the action code in
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
But the action is performed when either text view is entered. Is there a method that informs when a certain text view is being used?
You can do it in two ways
first
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if ( textView == YourTextView )
{
// ----- do here
}
return YES;
}
Second is
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if ( yourTextView.tag == yourTag)
{
// ----- do here
}
return YES;
}
You can set the tag of your UITextView to differentiate them.
// Somewhere in your code
UITextView *firstTextView = // set up your text view
firstTextView.tag = 0;
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// ...
if (textView.tag == 0) {
// firstTextView
}
// ...
}
Its for this purpose itself textView is passed as an argument to the delegate function
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
You can maintain properties for the textview instances and within the delegate call just check which of the textview received the touch.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Moving the cursor to the beginning of UITextField
Hello i have a textview with text I want to move cursor position at beginning I have use NSMakeRange but i don't know why its not working. I have written NSMakeRange is different places , hoping that it would run atleast once but didn't work. Here is the code. thx in advance
- (void)viewDidLoad
{
[super viewDidLoad];
apnatxtView.textColor=[UIColor lightGrayColor];
apnatxtView.text=#"Description goes here";
totalLenght=apnatxtView.text.length;
apnatxtView.selectedRange=NSMakeRange(0,0);
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
apnatxtView.selectedRange=NSMakeRange(0,0);
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[apnatxtView resignFirstResponder];
}
- (void)textViewDidChange:(UITextView *)textView{
if (apnatxtView.text.length == 0) {
apnatxtView.textColor= [UIColor lightGrayColor];
apnatxtView.text=#"Description goes here";
apnatxtView.selectedRange=NSMakeRange(0, 0);
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (apnatxtView.textColor == [UIColor lightGrayColor]) {
apnatxtView.textColor=[UIColor blackColor];
// apnatxtView.text=#"Description goes here";
apnatxtView.text=nil;
return YES;
}
}
This works in my testing on the iOS 6.0 simulator:
- (void)textViewDidBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
textView.selectedRange = NSMakeRange(0, 0);
});
}
I guess it updates the selection based on the touch location after it sends the textViewDidBeginEditing: message. The dispatch_async works around that.
K i found the solution. Im setting the cursor position before cursor appears I cut and paste the code in KeyBoardDidShow notification and it worked pretty fine.