2012-03-06 197 views
8

我是Objective-C的新手,我正在尋求限制用戶從普通鍵盤的字母部分切換到數字/標點符號端。這就是說,我想禁用鍵盤上的開關按鈕。也許我輸入了錯誤的搜索參數,但是我在Google上發現的很少,在我的參考書中更少。我如何去實際禁用iOS鍵盤上的按鈕?對於字母/數字/標點字符,只需忽略輸入的輸入字符即可輕鬆完成此操作,但在鍵盤部分之間切換的按鈕會執行操作而不是返回字符。禁用鍵盤上的鍵

謝謝你的幫助!

回答

4

實際上,你可以添加和刪除默認UIKeyboard

的按鈕有互聯網這樣的一些食譜:http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html像這樣:http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html

那些帖子,不過告訴你如何添加一個按鈕,同樣的原則可以用來消除。

下面我會告訴你的解決方案之一的編譯:

//The UIWindow that contains the keyboard view - 
//It some situations it will be better to actually 
//iterate through each window to figure out where the keyboard is, 
// but In my applications case 
//I know that the second window has the keyboard so I just reference it directly 
// 
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] 
//      objectAtIndex:1]; 

//Because we cant get access to the UIKeyboard throught the SDK we will 
// just use UIView. 
//UIKeyboard is a subclass of UIView anyways 
UIView* keyboard; 

//Iterate though each view inside of the selected Window 
for(int i = 0; i < [tempWindow.subviews count]; i++) 
{ 
    //Get a reference of the current view 
    keyboard = [tempWindow.subviews objectAtIndex:i]; 

    //Check to see if the className of the view we have 
    //referenced is "UIKeyboard" if so then we found 
    //the keyboard view that we were looking for 
    if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
    { 
     // Keyboard is now a UIView reference to the 
     // UIKeyboard we want. From here we can add a subview 
     // to th keyboard like a new button 

     //Do what ever you want to do to your keyboard here... 
    } 
} 
+0

謝謝!我得看看這個。從Java和C/C++背景來看,有時候Objective-C的做法似乎有點奇怪。 – alownx 2012-03-07 20:47:39

1

我實現了一個整潔的解決方案。訣竅是將禁​​用的關鍵圖像放在鍵盤上。

爲此

  1. 運行仿真器(在100%比例)和屏幕抓住你想要的資產(對我來說,這是在底部右端禁用完成按鈕)
  2. 在這張畫上的鍵盤

注意,鍵盤被放置在一個單獨的UIWindow(因爲iOS5中,我相信),因此頂部,你需要做以下

+ (void) addSubviewToTop:(UIView *)view { 
    int count = [[[UIApplication sharedApplication] windows] count]; 
    if(count <= 0) { 
     warn(@"addSubviewToTop failed to access application windows"); 
    } 
    UIWindow *top_window = [[[UIApplication sharedApplication] windows] objectAtIndex:count-1]; 
    [top_window addSubview:view]; 
    [top_window bringSubviewToFront:view]; 
} 
0

我不確定SDK是否發生了變化,以至於@ ppaulojr的答案不再有效,或者我的系統中有些東西是奇怪地設置的,但是通過以下調整,我能夠實現它!

在@ ppaulojr的回答中鏈接的帖子很棒(http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.htmlhttp://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html),他們幫助我得到這個工作。

顯然,實際的鍵盤視圖現在作爲子視圖嵌入到一些更宏觀的UIKeyboard視圖結構中,因此涉及到一些遞歸。我得到這個工作:

-(void) findKeyboard { 

    NSArray* windows = [[UIApplication sharedApplication] windows]; 

    for (int i = 0; i < [windows count]; i++) { 

     UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] 
                  objectAtIndex:i]; 

     for(UIView *subView in [tempWindow subviews]) 
     { 
      [self checkViews:subView]; 
     } 
    } 
} 

-(void)checkViews:(UIView *)inView 
{ 
    for(UIView *keyboard in inView.subviews) 
    { 
     NSLog(@"ViewName: %@", [keyboard description]); // Which view are we looking at 

     //Check to see if the className of the view we have 
     //referenced is "UIKeyboard" if so then we found 
     //the keyboard view that we were looking for 
     if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
     { 
      // Keyboard is now a UIView reference to the 
      // UIKeyboard we want. From here we can add a subview 
      // to th keyboard like a new button 

      //Do what ever you want to do to your keyboard here... 


      break; 
     } 

     // Recurse if not found 
     [self checkViews:subView]; 
    }  
} 

我還發現,調用此函數最好的地方是從-(void)textViewDidBeginEditing:(UITextView *)textView就像這樣:

- (void)textViewDidBeginEditing:(UITextView *)textView { 
    NSLog(@"textViewDidBeginEditing"); 

    [self findKeyboard]; 

} 

這隻要鍵盤被添加到做鍵盤修改窗口,但在它實際顯示之前,以便它從底部提升的整個時間,它將被修改。