2009-07-19 82 views
0

我使用這個技巧來爲數字鍵盤放置一個自定義按鈕。按鈕沒有刪除removedFromSuperVire

但是在使用後我不能刪除按鈕,所以在常規鍵盤上出現在視圖頂部。

這是我如何添加:

- (void)keyboardShow:(NSValue *)v 
{ 
    if (isKeyboardNumeric) { 
     // create custom button 
     UIButton *doneButton = [[UIButton alloc] init]; 
     //doneButton.buttonType = 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(dismissKeyboard:) forControlEvents:UIControlEventTouchUpInside]; 
     doneButton.tag = 99; 
     // 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]; 
     } 
    } 
} 

這我怎麼刪除它:

- (void)keyboardHide 
{ 
    UIView *btn; 
    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) { 
      for (btn in keyboard.subviews) { 
       if (btn.tag==99) { 
        [btn removeFromSuperview]; 
        [btn release]; 
        break; 
       } 
      } 
     } 
    } 
    [self setupButtons]; 
} 

這兩種方法都得到正確調用,並在調試我可以證實,被稱爲

[btn removeFromSuperview] 

但無論如何,該按鈕仍然存在。

我試圖把代碼刪除它只是在if (isKeyboardNumeric)調用之前,但沒有。

回答

4

這正是諸如此類的事情,蘋果告訴你不這樣做,並清除你知道,因爲你正試圖通過檢查OS版本,等看守自己這是超級脆弱的,是完全可能的,這將更改次要更新或錯誤修正。在2.x蘋果添加和修改了幾個鍵盤。

事實上,你無法獲得明智的行爲並不奇怪,誰知道蘋果在鍵盤視圖內正在做什麼,他們可能會重新制作或執行自定義繪製緩存以關閉屏幕圖像以提高繪圖效率。

如果你想這樣做,你應該實現自己的自定義數字鍵盤和彈出它進出,當您需要它。可能不是您想要聽到的答案,但它可能最終比在視圖中竊聽更簡單,並且不太可能爲您的用戶造成問題。

+0

好的,這是公平的。 我改變方式的用戶界面的工作,並刪除使用工具欄上,頂部的鍵盤和使用該視圖中的黑客。我把按鈕需要在導航窗口中。我希望測試版用戶更喜歡它;) – mamcx 2009-07-20 17:12:16