Moving the cursor once max length is reached - objective-c

I've crudely put together this code based on some other answers on SO but I still can't get it to work. I want my cursor to move to the next textField once a certain length has been reached, and it does that...to an extent.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == 0 && range.location >= 1) {
[_secondTextField becomeFirstResponder];
return YES;
} else if (textField.tag == 1 && range.location >= 2) {
[_thirdTextField becomeFirstResponder];
return YES;
} else if (textField.tag == 2 && range.location >= 1) {
[_fourthTextField becomeFirstResponder];
return YES;
} else {
return YES;
}
}
...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// catched the delegates for the input textfields
_firstTextField.delegate = self;
_secondTextField.delegate = self;
_thirdTextField.delegate = self;
[_firstTextField becomeFirstResponder];
}
So I have 4 textfields, and I have character limits of 1, 2, and 1 for the first 3 text fields. The problem right now is the cursor will not move to the next textfield until after I type more text. This next text is in the new textfield, so the overall effect is achieved, but the cursor just isn't moving at the right time.
What can I do so that the cursor will move as soon as I reach the character limit?

A better way would be to use the didChange method like the UITextViewDelegate method, but as we know the UITextFieldDelegate does not have a didChange method. You can manually add behaviour. You can use the shouldChangeCharactersInRange: method, but personally I would advise not to override methods unless you absolutely have to.
You can add behaviour using:
[yourTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
And in the target method:
- (void)textFieldDidChange:(UITextField*)textField{
if (textField.tag == 0 && textField.text.length == 1){
[_secondTextField becomeFirstResponder];
}else if (textField.tag == 1 && textField.text.length == 2){
[_thirdTextField becomeFirstResponder];
}else if (textField.tag == 2 && textField.text.length == 1){
[_fourthTextField becomeFirstResponder];
}
}

Related

Webpages loaded in WKWebView does not respond to keypress events

I want to enable keyboard shortcuts like Cmd+c , Cmd+v to work on webpages loaded inside WKWebView, but cannot find a solution.
Webview on the other hand has methods like Copy/Paste/Cut which could be leveraged by overriding (void)keyDown:(NSEvent *)event method. But WKWebView does not seem to have any similar funtion.
In WebView i could do something like
- (void)keyDown:(NSEvent *)event {
NSString *s = [event charactersIgnoringModifiers];
if (s == nil)
return;
if (([event modifierFlags] & NSDeviceIndependentModifierFlagsMask) == NSCommandKeyMask)
{
if ([s caseInsensitiveCompare:#"a"] == NSOrderedSame)
{
if ([[self firstResponder] respondsToSelector:#selector(selectAll:)])
[self.contentView selectAll:self];
}
else if ([s caseInsensitiveCompare:#"c"] == NSOrderedSame)
{
if ([[self firstResponder] respondsToSelector:#selector(copy:)])
[self.contentView copy:self];
}
else if ([s caseInsensitiveCompare:#"v"] == NSOrderedSame)
{
if ([[self firstResponder] respondsToSelector:#selector(paste:)])
[self.contentView paste:self];
}
else if ([s caseInsensitiveCompare:#"x"] == NSOrderedSame)
{
if ([[self firstResponder] respondsToSelector:#selector(cut:)])
[self.contentView cut:self];
}
else if ([s caseInsensitiveCompare:#"z"] == NSOrderedSame)
{
[self.contentView undo];
}
}
else
{
[super keyDown:event];
} }
In the above code self.contentView points to a WebView object

Which count max char in UITextField and UITextView

Which count maximum char's in UITextField and UITextView? It is really interesting for me! I have some textfields and textviews. And if I don't set max value for it, then which count char's can be in it?
You need to implement UITextFieldDelegate and UITextViewDelegate Method to get the count of characters
Try this code
For UITextView's
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSUInteger enteredTextLength; // Variable to store the length on entered string.
if(textView == txtViewName) {
enteredTextLength = [textView.text length] + [text length] - range.length;
NSLog(#"Text Length = %d", enteredTextLength);
// To check number of characters entered.
if (enteredTextLength < 10) {
return YES;
}
else {
return NO;
}
}
else {
return YES;
}
}
For UITextField's
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger enteredTextLength; // Variable to store the length on entered string.
if(textField == txtAlpha) {
enteredTextLength = [textField.text length] + [string length] - range.length;
NSLog(#"Text Length = %d", enteredTextLength);
// To check number of characters entered.
if (enteredTextLength < 10) {
return YES;
}
else {
return NO;
}
}
else {
return YES;
}
}

Objective C: Code Understanding

I have the following code:
- (IBAction)buttonSectionPressed:(id)sender {
if ([self.switchReloadOnlyDontToggleVissibility isOn]) {
[self updateCells:self.section2Cells];
} else {
BOOL hide = ([sender tag] == 0);
[self cells:self.section2Cells setHidden:hide];
}
[self reloadDataAnimated:([self.switchAnimated isOn])];
}
I have a question with
BOOL hide = ([sender tag] == 0);
Is it checking to see if (sender.tag == 0) then assign it to hide? So, (if sender.tag != 0), hide does not exist?
This expression works as follows:
Evaluates [sender tag]
Compares the result to zero
If the result is zero, hide is set to YES; otherwise, it is set to NO.
This could also be done with the equivalent expression that uses property syntax:
BOOL hide = (sender.tag == 0);
Finally, you can drop the hide variable altogether:
[self cells:self.section2Cells setHidden:(sender.tag == 0)];

Call a method from within a sibling method with parameters

I have followed the first steps tutorial from apple, and I have a working app that can read text and display it in a label on button click. When I hit enter the textfield is resigned and the keyboard disappears, and when I hit the button the changeGreeting method is invoked. I want to call the changeGreeting function in the function thats used when I hit enter which is textFieldShouldReturn.
I tried everything I could think of, and read a lot online but Im not sure how to deal with (id)sender as a param for example. How should I edit my code to call changeGreeting on textfield enter?
code below:
- (IBAction)changeGreeting:(id)sender {
self.userName = self.textField.text;
NSString *nameString = self.userName;
NSString *endString = #"burp";
int r = arc4random() % 74;
if ([nameString length] == 0) {
nameString = #"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:#"Hello, %#, %#! Random String of numbers: %d", nameString, endString, r];
self.label.text = greeting;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.textField) {
[theTextField resignFirstResponder];
// on enter the keyboard is removed, but I want the
// changeGreeting method involed too, something like
// [self changeGreeting]
}
return YES;
}
thanks in adv
You already have it. Just ignore the sender parameter:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.textField) {
[theTextField resignFirstResponder];
// on enter the keyboard is removed, but I want the
// changeGreeting method involed too, something like
[self changeGreeting:nil]
}

Hide Copy and Deselect UITextView options after copying all text

I am working on a messaging app. I want to give a "copy" option to the user when they enter their message in a UITextView. When the user presses the "copy" button, it is copying the message, but the popover shows again and again, and the text is still selectable.
I don't know how to control this. I have pasted some source code for your reference.
I wrote a sub class for UITextView.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
NSLog(#"Action : %#", NSStringFromSelector(action));
NSLog(#"Sender : %#", sender);
if (action == #selector(copy:))
{
[self selectAll:self];
//return [super canPerformAction:action withSender:sender];
return YES;
}
else if (action == #selector(cut:))
{
return NO;
}
return NO;
}
I have solved my problem. I have used below codes to solve.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(copy:))
{
[self selectAll:self];
return YES;
}
else if (action == #selector(cut:))
{
return NO;
}
return NO;
}
- (void)copy:(id)sender
{
UIPasteboard *pastBoard = [UIPasteboard generalPasteboard];
[pastBoard setString:self.text];
self.selectedTextRange = nil;
[self resignFirstResponder];
}
Thanks to Mr.Vimal Venugopalan and Mr.Mrueg. It is working for me. It will help to some one.
If you are using the iOS5
UITextView adopts the UITextInput protocol, which has a selectedTextRange property. Set the property to nil:
Add the below code just above the last return NO.
self.selectedTextRange = nil;
Hope this helps