2012-01-18 44 views
1

我在我的程序中創建了2 UITextView。他們將不得不這樣做:爲iPhone創建新的鍵盤按鈕 - InputAccessoryView和InputView中的Bug

  • TextView1 - 將有一個新的InputView
  • TextView2 - 會是標準的數字鍵盤的新按鈕

由於iPhone的數字鍵盤有一個空按鈕在零的左邊。所以,我可以在那裏放置一個UIView來模擬一個新的鍵盤。所以,我創建了UIToolbar一個UIView,這個按鈕並在TextView中有一個簡單的

[textView setInputAccessoryView:myView];  

設置要抓住這個視圖中點擊是有點複雜。我有子類principalClassName在main.m文件

int retVal = UIApplicationMain(argc, argv, MyPrincialClass ,MyDelegateClass); 

然後用

@interface MyPrincialClass : UIApplication 
- (void)sendEvent:(UIEvent *)anEvent; 
@end 
@implementation MyPrincialClass 
- (void)sendEvent:(UIEvent *)event { 
    [super sendEvent:event]; 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = touches.anyObject; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"click" object:(id)touch]; 
} 
@end 

創建該類現在只有聽此通知,並檢查該CLICK爲正確的區域。

因爲我有一個UINavegationBar,我必須去「Next」和「Previous」TextView。 OK,這只是運行

[textView1 resignFirstResponder]; 
[textView2 becomeFirstResponder]; 

如果所有的textView有相同的inputView(鍵盤)比按鈕工作正常。但是...因爲這個textView有一個inputView,比我的問題開始。

如果我先用一個按鈕在正常的UITextView中點擊,比按鈕顯示正確。紅色方塊在鍵盤上方,所以它工作。

work fine

如果首次點擊與定製inputView的UITextView中,按鈕顯示正確再次

I will not use this way, but work

但現在,如果用工具欄navegate(有一個簡單的resignFirstResponderbecomeFirstResponder)按鈕將顯示在鍵盤下

Fail... Fail again...

在resume ...所有的問題都只是因爲我使用了自定義的inputview。如果我只使用鍵盤,我不會有任何問題。

爲了解決這個問題,只在鍵盤上方放置了自定義按鈕的UIView。我嘗試獲取主UIWindows併發送消息:

[[UIApplication sharedApplication].keyWindow bringSubviewToFront:myview]; 

但是不行。任何想法解決這個錯誤?

回答

0

我修復它。這個想法是找到鍵盤的UIView並像子視圖一樣添加按鈕。這是代碼:

- (void)keyboardDidShow { 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard; 
    for(UIView* potentialKeyboard in tempWindow.subviews) { 
     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
      if([[potentialKeyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) { 
       keyboard = potentialKeyboard; 
      } else { 
       if([[potentialKeyboard description] hasPrefix:@"<UIKeyboard"] == YES) { 
        keyboard = potentialKeyboard; 
       } 
      } 
     } 
    } 
    [keyboard addSubview:key]; 
    [keyboard bringSubviewToFront:key]; 
} 
+0

嗯......不知道,但我有一個強烈的感覺,如果你提交到這個應用商店可能會被拒絕。不要以爲你應該像他們的內部視角結構一樣。你想用這個做什麼,你只需要一個按鈕,它是什麼? – 2012-01-29 00:58:00

+0

這很糟糕。 __WHY__ if語句_side_ for循環?當沒有鍵盤被發現時,這會崩潰。 – Joost 2012-01-29 21:19:31

+0

@Joost如果是因爲3.2以上的版本改變了有鍵盤的對象的名字。這有2個名字。是的,如果將來蘋果更改名稱,我會遇到麻煩...謝謝,我會修復它 – Rodrigo 2012-01-30 12:16:52

0

我希望我能正確理解你,我建議你管理按鈕的Z位置。因此它可以出現在所有組件之上。爲了方便我建議你下面的開源,Manage Z Order of Views。 希望這會幫助你,如果不是,請讓我知道。

乾杯, Arun。

+1

不要工作.... – Rodrigo 2012-01-25 16:36:37