如何在viewController中使用多个文件来访问它们?

时间:2017-07-05 06:46:06

标签: ios iphone swift

我正在尝试以编程方式创建一个Stackview,其中包含一个TextField说" Hello world"

class stackview: UIViewController {

    func setupview() {

        // Define TextField
        let Textfield = UITextView()
        Textfield.allowsEditingTextAttributes = false
        Textfield.translatesAutoresizingMaskIntoConstraints = false
        Textfield.widthAnchor.constraint(equalToConstant: 70).isActive = true
        Textfield.heightAnchor.constraint(equalToConstant: 70).isActive = true
        Textfield.isEditable = false

        // Define the stackview, and setting up the attributes
        let stackview = UIStackView()
        stackview.axis = UILayoutConstraintAxis.horizontal
        stackview.distribution = UIStackViewDistribution.equalCentering
        stackview.alignment = UIStackViewAlignment.center
        stackview.spacing = 5
        stackview.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackview)
        stackview.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        stackview.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        Textfield.text = "hello"

        view.addSubview(Textfield)

    }

在我的Viewcontroller.swift中,我想在ViewDidLoad中访问此函数

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var level = stackview()
        level.setupview()
    }

但Hello world dosnt似乎出现了!

2 个答案:

答案 0 :(得分:0)

做这样的事情。

<强> stackview.swift

import UIKit

class stackview: UIViewController {

    func setupview(myView: UIView) {

        // Define TextField
        let Textfield = UITextView()
        Textfield.allowsEditingTextAttributes = false
        Textfield.translatesAutoresizingMaskIntoConstraints = false
        Textfield.widthAnchor.constraint(equalToConstant: 70).isActive = true
        Textfield.heightAnchor.constraint(equalToConstant: 70).isActive = true
        Textfield.isEditable = false

        // Define the stackview, and setting up the attributes
        let stackview = UIStackView()
        stackview.axis = UILayoutConstraintAxis.horizontal
        stackview.distribution = UIStackViewDistribution.equalCentering
        stackview.alignment = UIStackViewAlignment.center
        stackview.spacing = 5
        stackview.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackview)
        stackview.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        stackview.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        Textfield.text = "hello"

        myView.addSubview(Textfield)
    }

<强> ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let level = stackview()
        level.setupview(myView: self.view)
    }
}

答案 1 :(得分:0)

您正在将视图控制器与视图,子视图和StackViews混合......

看看这种方法:

class ViewController: UIViewController {

    // define a UITextView and its properties
    var theTextView: UITextView = {
        let textView = UITextView()
        textView.allowsEditingTextAttributes = false
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.widthAnchor.constraint(equalToConstant: 70).isActive = true
        textView.heightAnchor.constraint(equalToConstant: 70).isActive = true
        textView.isEditable = false
        return textView
    }()

    // define a UIStackView and its properties
    var theStackView: UIStackView = {
        let stackview = UIStackView()
        stackview.axis = UILayoutConstraintAxis.horizontal
        stackview.distribution = UIStackViewDistribution.equalCentering
        stackview.alignment = UIStackViewAlignment.center
        stackview.spacing = 5
        stackview.translatesAutoresizingMaskIntoConstraints = false
        return stackview
    }()


    override func viewDidLoad() {
        super.viewDidLoad()

        // add the Stack View to this view - must be done before adding relative constraints
        self.view.addSubview(theStackView)

        // add relative constraints to the Stack View
        theStackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        theStackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        // set the text of the Text View
        theTextView.text = "hello"

        // add the Text View as an Arranged Subview of the Stack View
        theStackView.addArrangedSubview(theTextView)
    }

}

现在,稍后在您的代码中,您可以使用theStackView变量/属性引用堆栈视图 - 用于添加其他已排列的子视图,例如:

theStackView.addArrangedSubview(anotherTextView)
theStackView.addArrangedSubview(anImageView)
theStackView.addArrangedSubview(aButton)

您可以使用theTextView变量/属性访问文本视图 - 例如,更改文本。