2015-05-30 52 views
0

我想在swift中使用一個計算器作爲UITextField的自定義輸入視圖。在Swift中添加自定義輸入視圖到UITextField

我有一個計算器作爲CalculatorViewController在我的應用程序中單獨定義。

我需要訪問我的InputViewController中的UITextField中的CalculatorViewController。

我定義的InputViewController以下屬性來訪問計算器視圖

var textFieldInputView : UIView!{ 

     let calculatorInputController = self.storyboard?.instantiateViewControllerWithIdentifier("calculatorVC") as! calculatorViewController 

     let calculatorInputView = calculatorInputController.view 

     return calculatorInputView 
} 

然後添加在文本框下面的代碼來訪問textFieldInputView

textField.inputView = textFieldInputView 

現在,當我點擊文本框時,按照下面的計算器彈出窗口,它不在鍵盤出現的位置。也沒有任何按鈕工作。

非常感謝,如果有人能告訴我如何得到這個固定的。

screenshot of custom input view

回答

1

解決此具體根據以下。

  1. 創建一個界面生成器 「keyboard.xib」 的所有UIButtonUILabel顯示。
  2. 鏈接的keyboard.xibcalculatorViewController
  3. 子類視圖控制器(比如DetailViewController),其中UITextFields形式存在的calculatorViewController的子類。
  4. 創建UIView按照下面的代碼

    var customkeyboardView : UIView { 
    
        let nib = UINib(nibName: "CalculatorView", bundle: nil) 
        let objects = nib.instantiateWithOwner(self, options: nil) 
        let cView = objects[0] as! UIView 
        return cView 
    } 
    
  5. 分配customkeyboardView作爲的UITextField的inputView如下

    textField.inputView = customkeyboardView 
    
指示
相關問題