0

I use

[textField becomeFirstResponder];

to set keyboard always appears, but the cursor is missing from UITextfield.

how can i set the cursor still show when the keyboard showing up???

#import "Utility.h"
#import "InputViewController.h"
#import "SmsViewController.h"

@implementation InputViewController
@synthesize label,subLabel,textField;
@synthesize input,min,max,inputNo,letterOnly;
@synthesize nextInputViewController;

static NSMutableArray* inputList = nil;
static NSCharacterSet* letterSet;
static NSCharacterSet* digitSet;

+ (NSMutableArray*) inputList {
    if (inputList == nil) {
        inputList = [[NSMutableArray alloc] init];
    }
    return inputList;
}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

   // [textField becomeFirstResponder];
    // initialize STATIC variables if neccessary
    if (inputList == nil) {
        inputList = [[NSMutableArray alloc] init];
    }
    if (letterSet == nil) {
        letterSet = [NSCharacterSet alphanumericCharacterSet];
    }
    if (digitSet == nil) {
        digitSet = [NSCharacterSet decimalDigitCharacterSet];
    }

    min = [[input objectForKey:@"Min"] intValue];
    max = [[input objectForKey:@"Max"] intValue];
    letterOnly = [[input objectForKey:@"AlphaOnly"] boolValue];

    label.text = [input objectForKey:@"Title"];
    if (letterOnly) {
        subLabel.text = [NSString stringWithFormat:@"Chú ý: Chỉ chấp nhận các ký tự A-Z a-z 0-9"];
    }
    else {
        subLabel.text = [NSString stringWithFormat:@"Chú ý: Chỉ chấp nhận các số từ 0-9"];
    }

    textField.secureTextEntry = [[input objectForKey:@"Secure"] boolValue];
    textField.keyboardType = letterOnly ? UIKeyboardTypeASCIICapable : UIKeyboardTypeNumbersAndPunctuation;
    if ([[input objectForKey:@"Input"] count] == 0) {
        textField.returnKeyType = UIReturnKeyDone;
    } else {
        textField.returnKeyType = UIReturnKeyNext;
    }

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // auto focus on textField for waiting input
    //[textField becomeFirstResponder];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [input release];
    [nextInputViewController release];
    [super dealloc];
}

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (theTextField == textField) {

        // check maximum length
        if ([textField.text length] > max) {
            return NO;
        }

        // check Letter only or Digit only
        if ([string length] == 1) {
            if (letterOnly) {
                if (![letterSet characterIsMember:[string characterAtIndex:0]]) {
                    return NO;
                }
            }
            else {
                if (![digitSet characterIsMember:[string characterAtIndex:0]]) {
                    return NO;
                }
            }
        }       
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    if (theTextField == textField) {
     //   [textField becomeFirstResponder];
        int length = [textField.text length];
        if (length < min) {
            [Utility showAlert:[NSString stringWithFormat:@"%@ phải có chiều dài tối thiểu là %d!", label.text, min]];
            return NO;
        }
        else if ([label.text isEqualToString:@"Nhập mã bí mật mới"] &&
                 [textField.text isEqualToString:[inputList objectAtIndex:(inputNo-1)]] ) {

            [Utility showAlertWithTitle:@"Không hợp lệ" Content:@"Số bí mật mới giống hệt số cũ!"];
            return NO;
        }
        else if ([label.text isEqualToString:@"Xác nhận mã bí mật mới"] &&
                 ![textField.text isEqualToString:[inputList objectAtIndex:(inputNo-1)]] ) {

            [Utility showAlertWithTitle:@"Không hợp lệ" Content:@"Mã mới nhập lại không khớp với mã mới nhập lần đầu!"];
            return NO;
        }
        else {
    //      [textField resignFirstResponder];
            [self acceptInput:nil];
        }       
    }

    return YES;
}

- (IBAction)acceptInput: (id)sender {
    int length = [textField.text length];


    if (length < min) {
        [Utility showAlert:[NSString stringWithFormat:@"%@ phải có chiều dài tối thiểu là %d!", label.text, min]];
        return;
    }

    if ([inputList count] == inputNo) {
        [inputList addObject:(NSString*)textField.text];
    }
    else {
        [inputList replaceObjectAtIndex:inputNo withObject:(NSString*)textField.text];
    }

    NSDictionary* nextInput = [input objectForKey:@"Input"];

    if ([nextInput count] == 0) {
        SmsViewController* smsViewController = [[SmsViewController alloc] initWithNibName:@"SmsViewController" bundle:nil];
        smsViewController.title = @"SMS";
        smsViewController.sms = [input objectForKey:@"SMS"];
        smsViewController.smsDetails = inputList;

        NSString* confirm = [smsViewController.sms objectForKey:@"Confirm"];
        [self.navigationController pushViewController:smsViewController animated:YES];
        if (confirm == nil || [confirm isEqualToString:@""]) {

            // special case         
            [smsViewController showSMSPicker:smsViewController];
        }

        [smsViewController release];
    }
    else {
        if (nextInputViewController == nil) {
            nextInputViewController = [[InputViewController alloc] initWithNibName:@"InputViewController" bundle:[NSBundle mainBundle]];
            nextInputViewController.title = self.title;
            nextInputViewController.input = nextInput;
            nextInputViewController.inputNo = inputNo + 1;
        }       
        [self.navigationController pushViewController:nextInputViewController animated:NO];

    /*
        InputViewController *ivController = [[InputViewController alloc] initWithNibName:@"InputViewController" bundle:[NSBundle mainBundle]];
        ivController.title = self.title;
        ivController.input = nextInput;
        ivController.inputNo = inputNo + 1;
        [self.navigationController pushViewController:ivController animated:YES];
        [ivController release];
    */
    }

}

@end
3
  • Please provide your code .... Maybe there is an issue in it Oct 15, 2012 at 9:41
  • The cursor always shows when the keyboard is shown BTW ... Oct 15, 2012 at 9:42
  • i have updated my question by adding codes
    – Ngo Ky
    Oct 15, 2012 at 10:14

1 Answer 1

4

Declare your [textField becomeFirstResponder]; where you hav declared all your textfield delegates like :-

 _textFileld =[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 280, 50)];
_textFileld.backgroundColor=[UIColor whiteColor];
_textFileld.borderStyle = UITextBorderStyleRoundedRect;
_textFileld.delegate=self;
_textFileld.clearButtonMode = UITextFieldViewModeAlways;
_textFileld.returnKeyType = UIReturnKeyDone;  
_textFileld.placeholder = GlobalButtonString;
[_textFileld becomeFirstResponder];

See if this works ?...

5
  • i didn't declare my textfield in deligate i just add it to my UIViewcontroller .xib file
    – Ngo Ky
    Oct 16, 2012 at 3:00
  • @NgoKy have u added the delegate from the xib file ?? Oct 16, 2012 at 4:43
  • id don't really know how can i recognize that?
    – Ngo Ky
    Oct 16, 2012 at 6:55
  • @NgoKy you have to connect the delegate from the xib file by right-clicking the UIView in the xib and pulling the delegate from the UIView to the File's Owner .... got it ?? Oct 16, 2012 at 7:10
  • yes. i did like that. can you show me how to define my textfield in deligate and how to connect with .xib
    – Ngo Ky
    Oct 17, 2012 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.