2009-12-27 50 views
3

好吧,我會盡力解釋這一點,我可以做到最好。我有一個iPhone應用程序,它有一個文本字段,用戶只能輸入數字。那裏沒有問題。但是,數字鍵盤上沒有完成按鈕,所以我不能讓它消失。我可以製作一個用戶按下的按鈕來關閉鍵盤,但我寧願有一個完成的按鈕,因爲屏幕是「忙碌」的。在新鍵盤加載時從數字鍵盤刪除完成按鈕

那麼,經過一番研究,我碰到了this。哇,這很好。但是,我還有另一個需要默認鍵盤的文本字段。每當鍵盤出現時,無論類型如何,它都有「完成」按鈕。

不好。

所以我做了一些更多的挖掘。仔細閱讀這些評論,人們提到了一種使用「Editing Did Begin」特性擺脫「完成」按鈕的方法,只在必要時調用「完成按鈕」代碼。我也完成了大部分工作。如果您鍵入數字字段,然後關閉鍵盤,然後鍵入正常字段,完成按鈕不會出現。

但是,有一個錯誤,仍然出現「完成」按鈕。如果您點擊數字字段,然後點擊正常字段,鍵盤永遠不會「消失」,所以即使它從數字鍵盤變爲普通鍵盤,該按鈕仍然存在。我想從視圖中刪除按鈕,但我不知道如何去解決這個問題。這是我的代碼...

//Done button for numpad 
- (void)keyboardWillShow:(NSNotification *)note { 
    if (showDoneButton == YES) 
    { 


    // create custom button 
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(0, 163, 106, 53); 
    doneButton.adjustsImageWhenHighlighted = NO; 
    if ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"3"]) { 
     [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 view found; add the custom button to it 
     if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
      [keyboard addSubview:doneButton]; 
    } 
     showDoneButton = NO; //This tells done button code to run or not 
    } 
} 

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

當NumPadfield觸發「編輯也開始」,它調用此函數:

- (IBAction)needNumberPad:(id)sender{ 
    showDoneButton = YES; 
} 

它確認完成按鈕所示。完成按鈕代碼完成後,變量(如上所示)被設置爲NO,所以如果您點擊默認文本字段,它將不會再次顯示。布爾變量默認設置爲NO。

需要做的事情是,如果您立即從編輯numPad字段跳轉到默認字段,done按鈕消失。我可以將代碼放在「編輯結束」時調用的函數中,但我不知道該放什麼。幫助將不勝感激,我已經得到了這麼多!

謝謝!

回答

1

請按照以下步驟操作

a。首先讓doneButton成爲你的類的實例變量,這將有助於你保持對按鈕的引用

b。在烏爾keyboardWillShow的開頭添加以下代碼:(NSNotification *)注意方法

if(!showDoneButton){ 
     if(doneButton){ 
[doneButton removeFromSuperview]; 
doneButton = nil; 
     } 
     return; 
} 

℃。添加通知keyBoardWillHide

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillHide:) 
              name:UIKeyboardWillHideNotification 
              object:nil]; 

d。在keyBoardWillHide方法執行以下操作

- (void)keyboardWillHide:(NSNotification *)note 
{ 
if(doneButton) 
{ 
    [doneButton removeFromSuperview]; 
    doneButton = nil; 
} 
} 

這應該做的伎倆

0

您可以使用下面的方法:

//This delegate notify when the user done from the textfield 
- (void) textFieldDidEndEditing:(UITextField *)textField{ 
    [yourTextField resignFirstResponder]; 
    [doneButton removeFromSuperview]; 
    doneButton = nil; 
} 

yourTextField指含有進行鍵盤上按鍵的文本字段。 上一個方法將從下一個鍵盤中刪除doneButton