如何访问从Swift中的另一个类扩展UIStackView的swift类变量?

时间:2018-04-04 19:44:32

标签: ios xcode macos cocoa swift4

我有一个名为Text.swift的类,在这个类我创建我的文本字段然后我的另一个类Buttons.swift,我想访问这个类中的文本,这样当我按下按钮时文本字段中的文本可以显示在textview区域。但我的问题是我无法调用Text类我尝试了一些东西,但后来假设它是零。我是swift的初学者,所以你能帮我打电话吗?

public class Text:UIStackView {

required public init(coder: NSCoder) {
    super.init(coder : coder)
    setUpTexts()
}

var textName = UITextField()
var textLastName = UITextField()
var textDepartment = UITextField()

private func setUpTexts(){
    //text field properties such as border width
    //then I added to my stack

}

}

类按钮:UIStackView {

var buttonSave = UIButton()
var buttonClear = UIButton()
var buttonCancel = UIButton()
var textView = UITextView()
var anotherClass : Text!

var arrayName = [String!]()
var arrayLastName = [String!]()
var arrayDepartment = [String!]()

required init(coder: NSCoder) {
    super.init(coder : coder)
    setUpButtons()



}



private func setUpButtons(){

    buttonSave.setTitle("Save", for: .normal)
    buttonClear.setTitle("Clear", for: .normal)
    buttonCancel.setTitle("Cancel", for: .normal)


    buttonSave.addTarget(self, action: #selector(Buttons.btnSave(_:)), for: .touchUpInside)
    buttonClear.addTarget(self, action: #selector(Buttons.btnClear(_:)), for: .touchUpInside)
    buttonCancel.addTarget(self, action: #selector(Buttons.btnCancel(_:)), for: .touchUpInside)

    textView.isEditable = false


    addArrangedSubview(textView)
    addArrangedSubview(buttonSave)
    addArrangedSubview(buttonClear)
    addArrangedSubview(buttonCancel)


}


@IBAction func btnSave(_ sender : UIButton){


    arrayName.append(self.anotherClass?.textName.text)
    arrayLastName.append(self.anotherClass?.textLastName.text)
    arrayDepartment.append(self.anotherClass?.textDepartment.text)

    var sumText = " "
    for item in 0..<arrayName.count{
        sumText = sumText + "\(arrayName[item]!) \(arrayLastName[item]!)   \(arrayDepartment[item]!)\n" //to print each person line by line
    }
    textView.text = sumText */

}

我想将我的文本添加到这些数组中。但它无法看到它。我看了很多stackoverflow问题,但我找不到合适的问题。

1 个答案:

答案 0 :(得分:0)

你可以尝试,当你从Buttons自定义UIStackView类创建一个对象时,你必须给它一个框架或适当的约束

public class Text: UIStackView {

    required public init(coder: NSCoder) {
        super.init(coder : coder)
        setUpTexts()
    }

    public  override init(frame: CGRect) {
        super.init(frame : frame)
        setUpTexts()

    }
    var textName = UITextField()
    var textLastName = UITextField()
    var textDepartment = UITextField()

    private func setUpTexts(){

        distribution = .fillEqually
        axis = .vertical
        textName.backgroundColor = UIColor.red
        textLastName.backgroundColor = UIColor.blue
        textDepartment.backgroundColor = UIColor.orange
        addArrangedSubview(textName)
        addArrangedSubview(textLastName)
        addArrangedSubview(textDepartment)

    }

}

public class Buttons : UIStackView {

    var buttonSave = UIButton()
    var buttonClear = UIButton()
    var buttonCancel = UIButton()
    var textView = UITextView()
    var anotherClass = Text()
    var arrayName = [String!]()
    var arrayLastName = [String!]()
    var arrayDepartment = [String!]()

    required public init(coder: NSCoder) {
       super.init(coder : coder)
       setUpButtons()
    }

    private func setUpButtons(){

          distribution = .fillEqually

        buttonSave.setTitle("Save", for: .normal)
        buttonClear.setTitle("Clear", for: .normal)
        buttonCancel.setTitle("Cancel", for: .normal)


        buttonSave.addTarget(self, action: #selector(Buttons.btnSave(_:)), for: .touchUpInside)
        buttonClear.addTarget(self, action: #selector(Buttons.btnClear(_:)), for: .touchUpInside)
        buttonCancel.addTarget(self, action: #selector(Buttons.btnCancel(_:)), for: .touchUpInside)

        textView.isEditable = false

        textView.backgroundColor = UIColor.green

        addArrangedSubview(anotherClass)
        addArrangedSubview(textView)
        addArrangedSubview(buttonSave)
        addArrangedSubview(buttonClear)
        addArrangedSubview(buttonCancel)



    }
    @IBAction func btnSave(_ sender : UIButton){


        arrayName.append(self.anotherClass.textName.text)
        arrayLastName.append(self.anotherClass.textLastName.text)
        arrayDepartment.append(self.anotherClass.textDepartment.text)

        var sumText = " "
        for item in 0..<arrayName.count{
            sumText = sumText + "\(arrayName[item]!) \(arrayLastName[item]!)   \(arrayDepartment[item]!)\n" //to print each person line by line
        }
        textView.text = sumText

    }

    @IBAction func btnClear(_ sender : UIButton){

    }

    @IBAction func btnCancel(_ sender : UIButton){

    }

}