数据输入的自定义视图的Swift示例(自定义应用程序内键盘) [英] A Swift example of Custom Views for Data Input (custom in-app keyboard)

查看:119
本文介绍了数据输入的自定义视图的Swift示例(自定义应用程序内键盘)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标



我想制作一个仅在我的应用中使用的自定义键盘,而不是需要安装的系统键盘。



我已阅读并尝试过的内容



文档






  • 这是一款基本的应用内键盘。可以使用相同的方法来制作任何键盘布局。以下是需要完成的主要事项:




    • 在.xib文件中创建键盘布局,其所有者是.swift文件包含 UIView 子类。

    • 告诉 UITextField 使用自定义键盘。

    • 使用委托在键盘和主视图控制器之间进行通信。



    创建.xib键盘布局文件




    • 在Xcode中转到文件>新建>文件...> iOS>用户界面>查看以创建.xib文件。

    • 我调用了我的Keyboard.xib

    • 添加所需的按钮。

    • 使用自动布局约束,这样无论键盘的大小如何,按钮都会相应调整大小。

    • 将文件所有者(不是根视图)设置为是Keyboard.swift文件。这是一个常见的错误来源。请参阅最后的注释。



    创建.swift UIView子类键盘文件




    • 在Xcode中,转到文件>新建>文件...> iOS>源> Cocoa Touch Class 以创建.swift文件。

    • 我打电话给我的是Keyboard.swift

    • 添加以下代码:

        import UIKit 

      //视图控制器将采用此协议(委托)
      //因此必须包含keyWasTapped方法
      协议KeyboardDelegate:class {
      func keyWasTapped(character:String)
      }

      class键盘:UIView {

      //此变量将被设置为视图控制器,以便
      //键盘可以向视图控制器发送消息。
      弱var委托:KeyboardDelegate?

      // MARK: - 键盘初始化

      必需init?(编码器aDecoder:NSCoder){
      super.init(编码器:aDecoder)
      initializeSubviews ()
      }

      覆盖init(frame:CGRect){
      super.init(frame:frame)
      initializeSubviews()
      }

      func initializeSubviews(){
      let xibFileName =Keyboard//不包括xib扩展名
      let view = NSBundle.mainBundle()。loadNibNamed(xibFileName,owner:self,options:nil )[0]为! UIView
      self.addSubview(view)
      view.frame = self.bounds
      }

      // MARK: - 来自.xib文件的按钮操作

      @IBAction func keyTapped(sender:UIButton){
      //当点击一个按钮时,将该信息发送到
      //委托(即视图控制器)
      self .delegate?.keyWasTapped(character:sender.titleLabel!.text!)//也可以发送标签值
      }

      }


    • 控制从.xib文件中的按钮拖动到 @IBAction 方法.swift文件将它们全部挂起。


    • 注意协议和委托代码。有关代表如何工作的简单说明,请参见



      确保根视图的自定义类为空。




      Goal

      I want to make a custom keyboard that is only used within my app, not a system keyboard that needs to be installed.

      What I have read and tried

      Documentation

      The first article above states:

      Make sure a custom, systemwide keyboard is indeed what you want to develop. To provide a fully custom keyboard for just your app or to supplement the system keyboard with custom keys in just your app, the iOS SDK provides other, better options. Read about custom input views and input accessory views in Custom Views for Data Input in Text Programming Guide for iOS.

      That is what led me to the second article above. However, that article did not have enough detail to get me started.

      Tutorials

      I was able to get a working keyboard from the second tutorial in the list above. However, I couldn't find any tutorials that showed how to make an in app only keyboard as described in the Custom Views for Data Input documentation.

      Stack Overflow

      I also asked (and answered) these questions on my way to answering the current question.

      Question

      Does anyone have a minimal example (with even one button) of an in app custom keyboard? I am not looking for a whole tutorial, just a proof of concept that I can expand on myself.

      解决方案

      This is a basic in-app keyboard. The same method could be used to make just about any keyboard layout. Here are the main things that need to be done:

      • Create the keyboard layout in an .xib file, whose owner is a .swift file that contains a UIView subclass.
      • Tell the UITextField to use the custom keyboard.
      • Use a delegate to communicate between the keyboard and the main view controller.

      Create the .xib keyboard layout file

      • In Xcode go to File > New > File... > iOS > User Interface > View to create the .xib file.
      • I called mine Keyboard.xib
      • Add the buttons that you need.
      • Use auto layout constraints so that no matter what size the keyboard is, the buttons will resize accordingly.
      • Set the File's Owner (not the root view) to be the Keyboard.swift file. This is a common source of error. See the note at the end.

      Create the .swift UIView subclass keyboard file

      • In Xcode go to File > New > File... > iOS > Source > Cocoa Touch Class to create the .swift file.
      • I called mine Keyboard.swift
      • Add the following code:

        import UIKit
        
        // The view controller will adopt this protocol (delegate)
        // and thus must contain the keyWasTapped method
        protocol KeyboardDelegate: class {
            func keyWasTapped(character: String)
        }
        
        class Keyboard: UIView {
        
            // This variable will be set as the view controller so that 
            // the keyboard can send messages to the view controller.
            weak var delegate: KeyboardDelegate?
        
            // MARK:- keyboard initialization
        
            required init?(coder aDecoder: NSCoder) {
                super.init(coder: aDecoder)
                initializeSubviews()
            }
        
            override init(frame: CGRect) {
                super.init(frame: frame)
                initializeSubviews()
            }
        
            func initializeSubviews() {
                let xibFileName = "Keyboard" // xib extention not included
                let view = NSBundle.mainBundle().loadNibNamed(xibFileName, owner: self, options: nil)[0] as! UIView
                self.addSubview(view)
                view.frame = self.bounds
            }
        
            // MARK:- Button actions from .xib file
        
            @IBAction func keyTapped(sender: UIButton) {
                // When a button is tapped, send that information to the 
                // delegate (ie, the view controller)
                self.delegate?.keyWasTapped(character: sender.titleLabel!.text!) // could alternatively send a tag value
            }
        
        }
        

      • Control drag from the buttons in the .xib file to the @IBAction method in the .swift file to hook them all up.

      • Note that the protocol and delegate code. See this answer for a simple explanation about how delegates work.

      Set up the View Controller

      • Add a UITextField to your main storyboard and connect it to your view controller with an IBOutlet. Call it textField.
      • Use the following code for the View Controller:

        import UIKit
        
        class ViewController: UIViewController, KeyboardDelegate {
        
            @IBOutlet weak var textField: UITextField!
        
            override func viewDidLoad() {
                super.viewDidLoad()
        
                // initialize custom keyboard
                let keyboardView = Keyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
                keyboardView.delegate = self // the view controller will be notified by the keyboard whenever a key is tapped
        
                // replace system keyboard with custom keyboard
                textField.inputView = keyboardView
            }
        
            // required method for keyboard delegate protocol
            func keyWasTapped(character: String) {
                textField.insertText(character)
            }
        }
        

      • Note that the view controller adopts the KeyboardDelegate protocol that we defined above.

      Common error

      If you are getting an EXC_BAD_ACCESS error, it is probably because you set the view's custom class as Keyboard.swift rather than do this for the nib File's Owner.

      Select Keyboard.nib and then choose File's Owner.

      Make sure that the custom class for the root view is blank.

      这篇关于数据输入的自定义视图的Swift示例(自定义应用程序内键盘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆