2012-03-06 20 views
8

Sono nuovo su Objective-C e sto cercando di limitare un utente a passare dalla porzione di alfabeto di una normale tastiera a quella numerica/di punteggiatura. Detto questo, vorrei disabilitare il pulsante sulla tastiera che fa lo switch. Forse sto mettendo i parametri di ricerca sbagliati, ma trovo poco su Google e ancora meno nei miei libri di riferimento. Come faccio a disattivare effettivamente un pulsante su una tastiera iOS? Con caratteri alfa/numerici/punteggiatura, ciò sarebbe facilmente fatto ignorando semplicemente i caratteri inseriti che sono stati immessi, ma il pulsante che commuta tra le porzioni di tastiera esegue un'azione anziché restituire un carattere.Disabilitazione tasti sulla tastiera

Grazie per il vostro aiuto!

risposta

4

È effettivamente possibile aggiungere e rimuovere i pulsanti di default UIKeyboard

ci sono alcune ricette su Internet in questo modo: http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html e in questo modo: http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html

Tali posti vi mostrerà come aggiungere un pulsante, ma il lo stesso principio può essere usato per rimuovere.

Qui di seguito vi mostrerò una raccolta di una delle soluzioni:

//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

Grazie! Dovrò dare un'occhiata a questo. Venendo da uno sfondo Java e C/C++, a volte il modo in cui Objective-C fa le cose sembra un po ', beh, strano. – alownx

1

ho implementato una soluzione più ordinato. Il trucco consiste nel posizionare un'immagine chiave disabilitata sulla parte superiore della tastiera.

Per fare questo

  1. Run emulatore (nel 100% di scala) e schermo afferrare l'asset che desideri (nel mio caso, si trattava di un pulsante disattivato Fatto a fine basso a destra)
  2. Mettere questa immagine sulla parte superiore della tastiera

Nota che la tastiera è posto in un'UIWindow separato (dal iOS5 credo) e, quindi, è necessario eseguire le seguenti operazioni

+ (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

Non sono sicuro che l'SDK sia cambiato in modo tale che la risposta di @ppaulojr non funzioni più, o se ho semplicemente impostato stranamente le cose sul mio sistema, ma con le seguenti modifiche sono riuscito a farlo funzionare!

I post collegati nella risposta di @ ppaulojr sono fantastici (http://www.iphonedevsdk.com/forum/iphone-sdk-development/6573-howto-customize-uikeyboard.html e http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html) e mi hanno aiutato a farlo funzionare.

Apparentemente la vista da tastiera attuale è ora incorporata come sottoscheda in una struttura di visualizzazione UIKeyboard più grande, quindi è implicata una ricorsione. Ho avuto questo lavoro:

-(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]; 
    }  
} 

Ho anche scoperto che il posto migliore per chiamare questa funzione è da -(void)textViewDidBeginEditing:(UITextView *)textView in questo modo:

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

    [self findKeyboard]; 

} 

Questo non fa le modifiche della tastiera non appena la tastiera viene aggiunto la finestra, ma prima che si presenti effettivamente, in modo che tutto il tempo che si alza dal basso, sarà stato modificato.

Problemi correlati