2010-10-30 43 views
0

我想在iPhone鍵盤上添加一些自定義圖像按鈕,當有人選擇我的UITextField時。在iPhone鍵盤上有默認的按鈕來返回(或關閉鍵盤)?我想將返回按鈕更改爲我自己的按鈕並執行相同的功能,以關閉鍵盤。當選擇UITextField時,在iphone鍵盤上添加自定義圖像按鈕

可能嗎?任何人都可以幫助我一些源代碼?

+0

查看下面的答案是數字鍵盤... – Sabby 2011-01-06 05:03:07

回答

2

是的,你可以做this.Here是源代碼

- (void)loadView { 
    self.view = [[UIView alloc] initWithFrame:[UIScreen 
mainScreen].applicationFrame]; 
    self.view.backgroundColor = [UIColor 
groupTableViewBackgroundColor]; 

    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 
26)]; 
    textField.borderStyle = UITextBorderStyleRoundedRect; 
    textField.keyboardType = UIKeyboardTypeNumberPad; 
    textField.returnKeyType = UIReturnKeyDone; 
    textField.textAlignment = UITextAlignmentLeft; 
    textField.text = @"12345"; 
    [self.view addSubview:textField]; 
     // add observer for the respective notifications (depending on 
the os version) if ([[[UIDevice 
currentDevice] systemVersion] 
floatValue] >= 3.2) { 
     [[NSNotificationCenter 
defaultCenter] addObserver:self 
               selector:@selector(keyboardDidShow:) 
                name:UIKeyboardDidShowNotification 
                object:nil];   } else {  [[NSNotificationCenter 
defaultCenter] addObserver:self 
               selector:@selector(keyboardWillShow:) 
                name:UIKeyboardWillShowNotification 
                object:nil];  } } 

- (void)addButtonToKeyboard { // create custom button  UIButton 
*doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(0, 163, 
106, 53); 
    doneButton.adjustsImageWhenHighlighted 
= NO; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) { 
     [doneButton setImage:[UIImage 
imageNamed:@"DoneUp3.png"] 
forState:UIControlStateNormal]; 
     [doneButton setImage:[UIImage 
imageNamed:@"DoneDown3.png"] 
forState:UIControlStateHighlighted]; 
    } 
else { 
    [doneButton setImage:[UIImage 
imageNamed:@"DoneUp.png"] 
forState:UIControlStateNormal]; 
     [doneButton setImage:[UIImage 
imageNamed:@"DoneDown.png"] 
forState:UIControlStateHighlighted]; 
    } [doneButton addTarget:self 
action:@selector(doneButton:) 
forControlEvents:UIControlEventTouchUpInside]; 
    // locate keyboard view  UIWindow* 
tempWindow = [[[UIApplication 
sharedApplication] windows] 
objectAtIndex:1]; UIView* keyboard; 
    for(int i=0; i<[tempWindow.subviews 
count]; i++) {   keyboard = 
[tempWindow.subviews objectAtIndex:i]; 
     // keyboard found, add the button 
     if ([[[UIDevice currentDevice] 
systemVersion] floatValue] >= 3.2) { 
      if([[keyboard description] 
hasPrefix:@"<UIPeripheralHost"] == 
YES) 
       [keyboard addSubview:doneButton];  } else {   if([[keyboard 
description] hasPrefix:@"<UIKeyboard"] 
== YES) 
       [keyboard addSubview:doneButton];  } } } 

- (void)keyboardWillShow:(NSNotification 
*)note { // if clause is just an additional precaution, you could also 
dismiss it  if ([[[UIDevice 
currentDevice] systemVersion] 
floatValue] < 3.2) {  [self 
addButtonToKeyboard]; } } 

- (void)keyboardDidShow:(NSNotification 
*)note { // if clause is just an additional precaution, you could also 
dismiss it  if ([[[UIDevice 
currentDevice] systemVersion] 
floatValue] >= 3.2) {  [self 
addButtonToKeyboard]; 
    } } 


- (void)doneButton:(id)sender { NSLog(@"doneButton"); 
    NSLog(@"Input: %@", textField.text); 
    [textField resignFirstResponder]; } 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); } 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; } 

- (void)dealloc { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [textField release]; 
    [super dealloc]; } 

希望這會幫助你。

更多信息:http://www.appsfor2012.com/2012/09/keyboard-with-custom-button.html

相關問題