2009-09-24 11 views
6

có thể hiển thị bàn phím trong ứng dụng iPhone mà không có chế độ xem văn bản không? hoặc tôi sẽ phải có một textview vô hình?Bàn phím iphone không có chế độ xem văn bản

nếu có, làm thế nào để bạn tạo lập trình văn bản một cách có lập trình và sau đó đưa lên bàn phím (mà không cần người dùng phải nhấn vào chế độ xem văn bản)? các ví dụ duy nhất tôi có thể tìm thấy trình tạo giao diện sử dụng ..

+0

tại sao bạn muốn 'đưa lên' bàn phím nếu không có chỗ nào để nhập văn bản? –

+0

có thể trùng lặp của [Cách kéo lên UIKeyboard mà không có UITextField hoặc UITextView?] (Http://stackoverflow.com/questions/1376120/how-to-pull-up-a-uikeyboard-without-a-uitextfield-or -uitextview) – JosephH

Trả lời

7

Cách duy nhất (hợp lệ) để hiển thị bàn phím là phải có trường văn bản trả lời đầu tiên. Bạn có thể ẩn nó và làm cho nó trả lời đầu tiên theo chương trình bằng cách gọi becomeFirstResponder trên trường văn bản ẩn.

Bạn có thể tạo một UITextView lập trình bằng cách làm một cái gì đó như thế này (giả aRect và xem tồn tại)

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

[textView becomeFirstResponder]; 
1

Cách công cụ này hoạt động là thông qua NSNotificationCenter xuất bản/đăng ký mô hình. Trước tiên, bạn cần phải sử dụng addObserver:selector:name:object:, sau đó bạn có thể thử làm this:

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

Nhưng tôi không chắc chắn những gì thông báo mà mình sẽ nhận được, hoặc sẽ cần phải đăng ký, để có được những giá trị ký tự bàn phím gõ. Chúc may mắn và hacking hạnh phúc :)

+0

Làm thế nào điều này sẽ đưa lên bàn phím? – klaaspieter

+0

Tôi không chắc chắn nếu nó sẽ, lừa là để tìm thông báo đúng để đăng – slf

+0

công cụ thông báo bàn phím hơn http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top -keyboard.html – slf

2

Sau khi tìm hiểu thêm, tôi tìm thấy this. Nó không chính thức, nhưng tôi cá là nó hoạt động.

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 là bạn của bạn:

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

Ví dụ cách sử dụng. Nhấn vào chế độ xem màu đỏ và xem đầu ra bảng điều khiển 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 là chìa khóa cho giải pháp.

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

Ví dụ về cách sử dụng. Nhấn vào chế độ xem màu đỏ và xem đầu ra bàn điều khiển 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)") 
    } 
}