0

我正在尝试快速使用计算器作为 UITextField 的自定义输入视图。

我在我的应用程序中单独定义了一个计算器作为 CalculatorViewController。

我需要访问 InputViewController 中的 UITextField 中的 CalculatorViewController。

我在 InputViewController 中定义了以下属性来访问计算器视图

var textFieldInputView : UIView!{

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

        let calculatorInputView = calculatorInputController.view

        return calculatorInputView
}

然后在 textField 中添加以下代码以访问 textFieldInputView

    textField.inputView = textFieldInputView

现在,当我单击 textField 时,弹出计算器,如下所示,它不在键盘出现的位置。也没有按钮工作。

如果有人能建议我如何解决这个问题,我们将不胜感激。

自定义输入视图的屏幕截图

4

1 回答 1

3

按照下面的方法解决了这个问题。

  1. 创建了一个界面构建器“ keyboard.xib ”,其中包含所有用于显示的UIButton和。UILabel
  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
    
于 2015-06-01T12:03:25.063 回答