0

我用

[textField becomeFirstResponder];

设置键盘总是出现,但 UITextfield 中缺少光标。

当键盘出现时,如何设置光标仍然显示???

#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
4

1 回答 1

4

声明您[textField becomeFirstResponder];在哪里声明了所有文本字段代表,例如:-

 _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];

看看这是否有效?...

于 2012-10-15T10:36:03.820 回答