2012-10-15 24 views
0

我用即使鍵盤出現,我如何在UITextfield中設置光標?通過代碼?

[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 
+0

請提供您的代碼....也許有其中的一個問題 – IronManGill

+0

光標始終顯示鍵盤顯示時BTW ... – IronManGill

+0

我已通過添加代碼更新了我的問題 –

回答

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

看看這個工程...

+0

我沒有聲明我的文本字段在刪除我只是將它添加到我的UIViewcontroller .xib文件 –

+0

有什麼建議嗎? –

+0

@NgoKy你有沒有添加xib文件中的委託? – IronManGill

相關問題