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
Related
I added a feedback box inside my IOS application, and I want it to take only text to submit the response from the user, but when I tried to enter a white spaces inside the box it took it as a text and accept the submitting! How can I prevent that?
Specify the UIViewController as the delegate to your text view (you can do this either programmatically or specify the delegate in Interface Builder); and
Your UITextViewDelegate method shouldChangeTextInRange needs to check to see if the string to be inserted contains a space:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].location != NSNotFound) {
return NO;
}
return YES;
}
I have a UITextView which I am using as text entry in a chat application. When the user presses return then I want to do some action e.g. save the chat message.
I haven't been able to find a solution that allows me to do this (lots for TextFields but not for TextView).
Here is the solution I am trying at the moment, which seems to be the most obvious I can find, but it isnt working, in debug I see that the method isn't touched:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
NSLog(#"Return pressed");
} else {
NSLog(#"Other pressed");
}
return YES;
}
In my chat .h file:
#interface ChatTableViewController : UITableViewController <UITextViewDelegate>
and viewdidload .m file
- (void)viewDidLoad {
[super viewDidLoad];
enterText.delegate = self;
// more
}
Incidentally if there is a better field to use as input in a chat program than UITextView please let me know.
Many thanks
... but it isn't working, in debug I see that the method isn't touched
Your problem obviously is that the delegate method is not called. Fix the delegate and use the code you already have: it's good.
Try this way. No need to go with delegates.
Add this event responder at where you initialize the textView.
[theTextView addTarget:self
action:#selector(targetMethodToPerformCustomOperation)
forControlEvents:UIControlEventEditingDidEndOnExit];
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];
}
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.
The keyboard that comes up with MFMailComposeViewController does not have any means to dismiss the keyboard once it comes up.
Does anyone have an idea of changing the keyboard. There are no UITextField exposed as you are actually in mail client at the time.
The mail composer isn't yours to modify, it is a system provided view controller which you are explicitly told not to modify in the docs:
Important: The mail composition interface itself is not customizable and must not be modified by your application. In addition, after presenting the interface, your application is not allowed to make further changes to the email content. The user may still edit the content using the interface, but programmatic changes are ignored. Thus, you must set the values of content fields before presenting the interface.
The cancel button is already there in the top left, what would "Done" do? Send the email? That's in the top right.
The MFMailComposeViewController doesn't have a "Done" button, because it assumes you will use that button as a return key (to make a new line).
If you really wanted to change the button to a "done" button, there is only one way I can think to do it:
Create a new MFMailComposeViewController.
Enumerate through [[mailComposer view] subviews].
Inspect each subview (and subviews of subviews, if required).
When you've found the UITextView that is the body, do the following:
// Get the UITextView from subview inspection
UITextView *textView;
// Declare this instance variable in your class #interface
id <UITextViewDelegate> originalTextViewDelegate;
// Get the original delegate
originalTextViewDelegate = [textView delegate];
// Override the delegate
[textView setDelegate:self];
// Set the return key type
[textView setReturnKeyType:UIReturnKeyDone];
Return YES on -textViewShouldEndEditing. Implement ALL UITextViewDelegate methods, and call originalTextViewDelegate (kind of like calling "super" on a subclass).
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
[originalTextViewDelegate textViewShouldEndEditing:textView];
// Important: return YES, regardless of originalTextViewDelegate's response
return YES;
}
- (void)textViewDidChangeSelection:(UITextView *)textView
{
[originalTextViewDelegate textViewDidChangeSelection:textView];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return [originalTextViewDelegate textView:textView shouldChangeTextInRange:range replacementText:text];
}
// etc
That should work, but no guarantees. Hope that helps! :D