本文介绍了当 UITextField 被点击时加载一个 nib 文件作为自定义键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载 .xib 文件以在点击 textField 时充当自定义键盘.我可以在点击 textField 时显示 .xib 文件(视图),但我不确定如何将 .xib 文件中的按钮与ViewController.swift 中的一个 textField.

这是我目前所做的.

  1. 创建了一个项目.

  2. 添加了一个 UITextField 并为其创建了一个插座.

    @IBOutlet 弱变量 myField:MyTextField!

  3. 创建了一个新的 .xib 文件并将其命名为 CustomView.xib.

  4. 创建了一个新类并将其命名为 CustomView.swift.

  5. CustomView.swift 类分配给 CustomView.xib 文件.

  6. CustomView.xib 文件中添加了一些 UIButtons.这些将充当自定义键盘,它们将在 textField 被点击时显示并在 resignFirstResponder 方法被调用时隐藏.

  7. ViewController.swiftviewDidLoad 方法中,我分配了 inputView 如下.

     导入 UIKit类视图控制器:UIViewController {@IBOutlet 弱 var myField:MyTextField!覆盖 func viewDidLoad() {super.viewDidLoad()//在加载视图后做任何额外的设置,通常是从笔尖.让 myView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as?自定义视图myField.inputView = myView}}

  8. 完成

当我运行应用程序并点击 .xib 文件显示的 textField 时(见下图),我的问题是,如何将按钮与 ViewController.swif 中的 textField 通信.换句话说,我想要的是在输入字段中点击它们时显示数字.

有什么建议吗?这是通常的做法吗.

这是一张图片,显示了点击 inputField 时显示的视图.

解决方案

假设你有 9 个 UIButton ,然后考虑 + 拖动一个 IBAction 从故事板中的所有这些按钮到单个方法,例如:

- (IBAction)tapped:(id)sender{textView.text = [NSString stringWithFormat:@"%@%li", textView.text, (long)((UIButton*)sender).tag];NSLog(@"%@", textView.text);}

鉴于 textField 是您的文本字段,然后您可以使用这些的 tag 属性从键盘(即按钮)附加每个相应的数字按钮和上述方法.

您可以为故事板中的每个按钮设置标签编号(即分别为 1 - 9).

我没有使用 9 个按钮进行测试,而是只使用了 2 个,每个按钮的标签编号分别为 1 和 2.结果在 UITextField(即112122 等)中显示良好.>

更新(评论后):

我能够使用包含几个按钮的 nib 文件和 storyboard 中的 UITextField 重新创建它.

流程如下:

1. 创建一个带有所有按钮的笔尖(您已经完成了).

2. 创建一个视图;并在自定义类"下,将类重命名为此视图(即自定义类"->类"->view-that-holds-the-buttons").

3.IBActions(总共 9 个对应于您的按钮数量)连接到一个单个方法,如上所述.

4. 在您的另一个视图控制器中,其视图包含 UITextField,使用您现有的方法加载笔尖.

5.将此视图(来自笔尖)添加为子视图.

以下关注视图(包含按钮和 IBAction 方法)和加载 nib 的控制器之间的通信:

6. 创建一个 delegate(weak)属性.

7. 在添加视图(来自 nib)之前,将此 delegate 分配给视图控制器(加载 nib 的控件)代码>笔尖).

8. 创建协议:

例如:

protocol keypadProtocol : 类{func displayKeys(keystrokeObject: AnyObject)}

让加载nib的视图控制器符合这个协议并实现所需的方法(displayKeys):

//加载笔尖的那个,也持有UITextField类 mainViewController: UIViewController, keypadProtocol

所以,一旦按钮被点击,IBAction 就会被调用;但是我们没有显示它,而是将 sender 发送到我们的 delegate,它是实现 displayKeys 方法并保存 的视图控制器>UITextField.

IBAction 将实现如下:

@IBAction func tapped(sender: AnyObject){委托!.displayKeys(sender)}

displayKeys 将如下实现:

func displayKeys(keystrokeObject: AnyObject){textView.text = NSString(format: "%@%li", textView.text!?? "", (keystrokeObject as! UIButton).tag) as String}

在加载 nib 文件的控制器中声明 delegate:

weak var 委托:keypadProtocol?

从加载 nib 的视图控制器中分配 delegate:

keyPadView.delegate = self//keyPadView是加载的nib文件

回复您的第二条评论:

假设:

我们有 2 个班级.

第一个是 UIView 的子类,它是 xib,它包含按钮.我们称之为KeypadView".

第二个是主视图控制器,与持有的控制器相关联故事板中的 UITextField.我们称之为MainViewController".

第 2 步:

首先,请创建一个新的 UIView 并将其命名为KeypadView".
然后,单击您的 .xib 文件;在右侧面板上,单击左侧第三个选项卡,称为身份检查器";您会看到自定义类 -> 类",您可以在其中将此 xib 关联到您创建的类(您需要这个类,在以便将 xib 文件中按钮的 IBAction 连接到它).它将是KeypadView",它是 UIView 的子类.

第 6 步:

您在包含按钮的类 (KeypadView") 中声明它.

第 8 步:

您将此方法 (IBAction) 连接到上述类(即KeypadView").

从mainViewController"中加载xib(KeypadView")后,将KeypadView"中的delegate设置为self(self 是M​​ainViewController"):

let keyPadView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as?键盘视图keyPadView.delegate = selfself.view.addSubview(keypadView)//您可能需要重新调整视图的位置;我会把它留给你

在您的KeyPadView"类中,应该有一个从每个按钮调用的 IBAction:

即,

@IBAction func tapped(sender: AnyObject){委托!.displayKeys(sender)}

我们的委托是mainViewController",如果你还记得的话.

由于 displayKeys 是在mainViewController"中实现的,因此将在其中调用以下方法:

func displayKeys(keystrokeObject: AnyObject){textView.text = NSString(format: "%@%li", textView.text!?? "", (keystrokeObject as! UIButton).tag) as String}

„mainViewController"然后将在其 UITextField(即 textView)中显示击键.

I'm trying to load a .xib file to act as a custom keyboard when a textField is tapped. I'm able to show the .xib file (view) when the textField is tapped but I'm not sure how to communicate the buttons in the .xib file with the a textField in the ViewController.swift.

This is what I have done so far.

  1. Created a single project.

  2. Added a UITextField and created an outlet for it.

    @IBOutlet weak var myField: MyTextField!
    

  3. Created a new .xib file and called it CustomView.xib.

  4. Created a new class and called it CustomView.swift.

  5. Assigned class CustomView.swift to the CustomView.xib file.

  6. Added some UIButtons in the CustomView.xib file. These will be acting as a custom-keyboard, they will show when the textField is tapped and hide when the resignFirstResponder method is called.

  7. In the viewDidLoad method of the ViewController.swift I assigned inputView as follow.

     import UIKit
    
     class ViewController: UIViewController {
    
      @IBOutlet weak var myField: MyTextField!
    
      override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let myView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as? CustomView
        myField.inputView = myView
        }
    }
    

  8. Done

When I run the app and tap on the textField the .xib file shows (see picture below), my problem is, how do I communicate the buttons with the textField in the ViewController.swif. In other words what I want is to show the numbers in the inputField when they are tapped.

Any suggestions? Is this how this is usually done.

Here is an image that shows the view shown when the inputField was tapped.

解决方案

Let's say you have 9 UIButtons, then consider + dragging an IBAction from all these buttons in storyboard to a single method such as:

- (IBAction)tapped:(id)sender
{
    textView.text = [NSString stringWithFormat:@"%@%li", textView.text, (long)((UIButton*)sender).tag];
    NSLog(@"%@", textView.text);
}

given that textField is your text field, you could then append each corresponding number from your keypad (that is to say, the buttons) using the tag property of these buttons and the aforementioned method.

You could set the tag number for each single button in storyboard (I.e., 1 - 9, respectively).

I didn't test it using 9 buttons but did with only 2, with tag numbers 1 and 2, respectively for each buttons. The result was displaying fine in UITextField (I.e., 1, 12, 122 and so forth).

Update (Post-comment):

I was able to re-create this using a nib file containing a few buttons and a UITextField in storyboard.

The process proceeds as follows:

1. Create a nib with all the buttons (which you already have done).

2. Create a view; and under "Custom Class", re-name the class to this view (I.e., "Custom Class" -> "Class" -> "view-that-holds-the-buttons").

3. Wire the IBActions (a total of 9 corresponding to the number of your buttons) to one single method as described above.

4. In your other view controller whose view hold the UITextField, load the nib using your existing method.

5. Add this view (from nib) as subview.

The following concerns with communication between the view (that holds the buttons along with the IBAction method) and the controller in which you load the nib:

6. create a delegate (weak) property.

7. Before you add the view (from nib), assign this delegate with the view controller (the control that loads the nib).

8. Create a protocol:

For instance:

protocol keypadProtocol : class
{
    func displayKeys(keystrokeObject: AnyObject)
}

Have the view controller that loads the nib conform to this protocol and implement the required method (displayKeys):

//The one that loads the nib, which also holds the UITextField
class mainViewController: UIViewController, keypadProtocol

So, once the buttons are tapped, the IBAction would be called; but instead of displaying it, we send the sender to our delegate, which is the view controller that implements the displayKeys method and holds the UITextField.

The IBAction would be implemented as follows:

@IBAction func tapped(sender: AnyObject)
{
     delegate!.displayKeys(sender)
}

displayKeys would be implemented like the following:

func displayKeys(keystrokeObject: AnyObject)
{
    textView.text = NSString(format: "%@%li", textView.text! ?? "", (keystrokeObject as! UIButton).tag) as String
}

Declaration of the delegate in the controller where you load the nib file:

weak var delegate : keypadProtocol?

Assigning the delegate from within the view controller where you load the nib:

keyPadView.delegate = self //keyPadView is the nib file loaded


In reply to your second comment:

Assumption:

We have 2 classes.

The first one is a subclass of UIView, which is the xib, that holds the buttons.Let’s call this „KeypadView".

The second one is the main view controller, which is associated to the controller that holdsthe UITextField in your storyboard.Let’s call this „MainViewController".

Step 2:

Firstly, please create a new UIView and name it, for the sake of consistency, „KeypadView".
Then, click on your .xib file; on the right panel, click on the third tab from the left, which is called „Identity Inspector"; you would see „Custom Class -> Class", where you would associate this xib to the class you created (you need this class, in order to connect the IBAction for the buttons from the xib file to it). It would be the „KeypadView", which is a subclass of UIView.

Step 6:

You declare this in the class („KeypadView") that holds the buttons.

Step 8:

You connect this method (IBAction) to the aforementioned class (I.e., „KeypadView").

Once you load the xib („KeypadView") from within the „mainViewController", set the delegate in „KeypadView" to self (self is the „MainViewController"):

let keyPadView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as? KeyPadView

keyPadView.delegate = self
self.view.addSubview(keypadView)

//you may need to re-adjust the position of the views; I will leave that to you

In your „KeyPadView" class, there should be an IBAction that gets called from each of the buttons:

I.e.,

@IBAction func tapped(sender: AnyObject)
{
    delegate!.displayKeys(sender)
}

Our delegate is the „mainViewController", if you recall.

Since displayKeys is implemented in „mainViewController", the following method would be called therein:

func displayKeys(keystrokeObject: AnyObject)
{
    textView.text = NSString(format: "%@%li", textView.text! ?? "", (keystrokeObject as! UIButton).tag) as String
}

„mainViewController" would then display the keystrokes in its UITextField (I.e., textView).

这篇关于当 UITextField 被点击时加载一个 nib 文件作为自定义键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:14