2009-09-24 5 views
6

est-il possible d'élever le clavier dans une application iphone sans textview? ou devrais-je avoir un textview invisible?clavier iphone sans textview

Si oui, comment créez-vous par programmation une vue de texte et ensuite afficher le clavier (sans que l'utilisateur doive appuyer sur la vue de texte)? Les seuls exemples que je peux trouver utilisent le constructeur d'interface ..

+0

Pourquoi voudriez-vous «élever» le clavier s'il n'y a pas de texte? –

+0

duplication possible de [Comment extraire un UIKeyboard sans UITextField ou UITextView?] (Http://stackoverflow.com/questions/1376120/how-to-pull-up-a-uikeyboard-without-a-uitextfield-ou -uitextview) – JosephH

Répondre

7

La seule façon (valable) d'afficher le clavier est d'avoir un champ de texte qui est le premier répondeur. Vous pouvez le masquer et le rendre premier répondeur par programmation en appelant becomeFirstResponder dans le champ de texte masqué.

Vous pouvez créer un programme UITextView en faisant quelque chose comme ceci (en supposant ARect et vue existent)

var textView = [[[UITextView alloc] initWithFrame:aRect] autorelease]; 
[view addSubview:textView]; 

[textView becomeFirstResponder]; 
1

La façon dont fonctionne ce genre de choses se fait via le modèle NSNotificationCenter publish/subscribe. Vous devez d'abord utiliser addObserver:selector:name:object:, vous pouvez essayer de faire this:

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:NSTextDidBeginEditingNotification object:self]]; 

Mais je ne suis pas sûr de ce que vous les notifications ne se ou aurait besoin d'inscrire, pour obtenir les valeurs de caractères de frappe du clavier. Bonne chance et bon hacking :)

+0

Comment cela fera-t-il monter le clavier? – klaaspieter

+0

Je ne sais pas si ce sera le cas, l'astuce consiste à trouver la bonne notification pour poster – slf

+0

plus de choses de notification de clavier http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top -keyboard.html – slf

2

Après quelques recherches supplémentaires, j'ai trouvé this. Ce n'est pas officiel, mais je parie que ça fonctionne.

UIKeyboard *keyboard = [[[UIKeyboard alloc] initWithFrame: CGRectMake(0.0f, contentRect.size.height - 216.0f, contentRect.size.width, 216.0f)] autorelease]; 
     [keyboard setReturnKeyEnabled:NO]; 
     [keyboard setTapDelegate:editingTextView]; 
     [inputView addSubview:keyboard]; 
1

UIKeyInput est votre ami:

protocol KeyboardInputControlDelegate: class { 
    func keyboardInputControl(keyboardInputControl:KeyboardInputControl, didPressKey key:Character) 
} 

class KeyboardInputControl: UIControl, UIKeyInput { 

    // MARK: - properties 

    weak var delegate: KeyboardInputControlDelegate? 

    // MARK: - init 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     addTarget(self, action: Selector("onTouchUpInside:"), forControlEvents: .TouchUpInside) 
    } 

    required init(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    // MARK: - UIView 

    override func canBecomeFirstResponder() -> Bool { 
     return true 
    } 

    // MARK: - methods 

    dynamic private func onTouchUpInside(sender: KeyboardInputControl) { 
     becomeFirstResponder() 
    } 

    // MARK: - UIKeyInput 

    var text:String = "" 

    func hasText() -> Bool { 
     return text.isEmpty 
    } 

    func insertText(text: String) { 
     self.text = text 
     for ch in text { 
      delegate?.keyboardInputControl(self, didPressKey: ch) 
     } 
    } 

    func deleteBackward() { 
     if !text.isEmpty { 
      let newText = text[text.startIndex..<text.endIndex.predecessor()] 
      text = newText 
     } 
    } 
} 

Exemple d'utilisation. Appuyez sur la vue rouge et voir la sortie de la console Xcode:

class ViewController: UIViewController, KeyboardInputControlDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let kic = KeyboardInputControl(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 
     kic.delegate = self 
     kic.backgroundColor = UIColor.redColor() 
     view.addSubview(kic) 
    } 

    func keyboardInputControl(keyboardInputControl: KeyboardInputControl, didPressKey key: Character) { 
     println("Did press: \(key)") 
    } 
} 
1

UIKeyInput est la clé de la solution.

protocol KeyboardInputControlDelegate: class { 
    func keyboardInputControl(keyboardInputControl:KeyboardInputControl, didPressKey key:Character) 
} 

class KeyboardInputControl: UIControl, UIKeyInput { 

    // MARK: - properties 

    weak var delegate: KeyboardInputControlDelegate? 

    // MARK: - init 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     addTarget(self, action: Selector("onTouchUpInside:"), forControlEvents: .TouchUpInside) 
    } 

    required init(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    // MARK: - UIView 

    override func canBecomeFirstResponder() -> Bool { 
     return true 
    } 

    // MARK: - methods 

    dynamic private func onTouchUpInside(sender: KeyboardInputControl) { 
     becomeFirstResponder() 
    } 

    // MARK: - UIKeyInput 

    var text:String = "" 

    func hasText() -> Bool { 
     return text.isEmpty 
    } 

    func insertText(text: String) { 
     self.text = text 
     for ch in text { 
      delegate?.keyboardInputControl(self, didPressKey: ch) 
     } 
    } 

    func deleteBackward() { 
     if !text.isEmpty { 
      let newText = text[text.startIndex..<text.endIndex.predecessor()] 
      text = newText 
     } 
    } 
} 

Exemple d'utilisation. Appuyez sur la vue rouge et voyez la sortie de la console Xcode:

class ViewController: UIViewController, KeyboardInputControlDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let kic = KeyboardInputControl(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 
     kic.delegate = self 
     kic.backgroundColor = UIColor.redColor() 
     view.addSubview(kic) 
    } 

    func keyboardInputControl(keyboardInputControl: KeyboardInputControl, didPressKey key: Character) { 
     println("Did press: \(key)") 
    } 
} 
Questions connexes