我应该如何将数据从 xib 发送到视图控制器? [英] how I should send data from xib to viewcontroller?

查看:20
本文介绍了我应该如何将数据从 xib 发送到视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xib视图,这个视图包含一个计算器的按钮,我这样做是因为我的应用程序需要一个自定义键盘,我的应用程序有一些需要这个键盘的视图问题是,我如何将数据从视图发送到我的视图控制器?在我的视图控制器中,我只有一个文本字段和一个带有 xib 自定义类的视图

i have a view that is a xib, this view contains a buttons for a calculator, I did this because I need a custom keyboard for my app, my app has some views that needs this keyboard the problem is, how I send the data from view to my view controller? in my view controller I only has a textfield and a view with xib custom class

我使用 swift 4.2

I use swift 4.2

自定义视图和文本传送

这是我的xib代码

import UIKit

class keyboardView: UIView {


@IBOutlet var viewKeyboard: UIView!

var textIntroduced = ""

override init(frame: CGRect) { // for using CustomView in code
    super.init(frame: frame)
    custom()
}

required init?(coder aDecoder: NSCoder)
{
    super.init(coder: aDecoder)
    custom()
}

private func custom()
{
    Bundle.main.loadNibNamed("keyboard", owner: self, options: nil)

    viewKeyboard.frame = self.bounds
    viewKeyboard.autoresizingMask = [.flexibleHeight,.flexibleWidth]
    for view in viewKeyboard.subviews
    {
        view.isExclusiveTouch = true
    }
    addSubview(viewKeyboard)
}

@IBAction func addOne(_ sender: Any) {
    textIntroduced += "1"
}
@IBAction func addTwo(_ sender: Any) {
    textIntroduced += "2"
}
@IBAction func addThree(_ sender: Any) {
    textIntroduced += "3"
}
@IBAction func addFour(_ sender: Any) {
    textIntroduced += "4"
}
@IBAction func addFive(_ sender: Any) {
    textIntroduced += "5"
}
@IBAction func addSix(_ sender: Any) {
    textIntroduced += "6"
}
@IBAction func addSeven(_ sender: Any) {
    textIntroduced += "7"
}
@IBAction func addEight(_ sender: Any) {
    textIntroduced += "8"
}
@IBAction func addNine(_ sender: Any) {
    textIntroduced += "9"
}
@IBAction func addZero(_ sender: Any) {
    textIntroduced += "0"
    print(textIntroduced)
}
@IBAction func removeNumber(_ sender: Any) {
    textIntroduced.removeLast()
}
}

在我的视图控制器中,我只有一个文本字段和带有自定义类的视图

in my view controller I only has a textfield and view with custom class

我想推送任何按钮视图,结果应该写在文本字段中.

i want to push any button view and the result should be written in the textfield.

推荐答案

您可以使用协议来观察您的 xib 中的数据变化.首先你需要创建一个这样的协议.

You can use protocol to observe data changes in your xib. First of all you need to create a protocol like this.

protocol NumberCalculable: class {
    func addNumber(_ number: Int)
}

然后,在 viewKeyboard 插座下方的 xib 文件中,您需要为您的协议创建一个委托.

Then, inside your xib file below your viewKeyboard outlet you need to create a delegate for your protocol.

weak var delegate: NumberCalculable?

你应该改变这个 delegate?.addNumber(1) 而不是做 textIntroduced += 1 和其他 IBAction 方法一样.第三,你需要在你的 viewController 类中符合你的协议keyboardView.delegate = selfviewDidLoad() 方法中.

Instead of doing textIntroduced += 1 you should change with this delegate?.addNumber(1) and same for other IBAction methods. Thirdly, you need to conform your protocol in your viewController class doing keyboardView.delegate = self inside viewDidLoad() method.

extension ViewController: NumberCalculable {
func addNumber(_ number: Int) {
// do whatever you want
}
}

希望这会有所帮助.

这篇关于我应该如何将数据从 xib 发送到视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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