2017-07-05 5 views
1

私はprogramically私はのviewDidLoadviewControllerで複数のファイルを使用してアクセスするにはどうすればよいですか?

class ViewController: UIViewController { 

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

が、こんにちは、世界でこの機能にアクセスしたい私のViewcontroller.swiftで "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) 

    } 

言っTextFieldを含まStackviewを作成しようとしていますdosntが表示されるようです!

+0

line.setupview()の後、親ビューにビューを追加する必要があります。self.view.addSubView(level.view)これを試してください –

答えて

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) 
    } 
} 
+0

なぜmyViewパラメータを作成する必要がありますか?私は単に私の関数内のビューを参照していますか? – user3211359

+0

現在のビューをstackview @ user3211359に渡す –

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変数/プロパティ - 例えばテキストを変更するためのものです。

関連する問題