Lachtan
Lachtan

Reputation: 5172

How to make UI elements in class out of View Controller

I'm making app and I'm trying to make a View which contains a Label with a question. I want this view in my app and because I will use it repeatedly, I made a class (If I want to make some change, I can do It from one place). The UIView is called questionView (var questionView = UIView()). Problem is when I want to make questionView a subview of view. The error says that I don't have have "view" which I understand. I don't have view but how can I get it? Thank you

This is what is inside my Question class:

import Foundation
import UIKit

class Question {

    // PROPERTIES:

    var questionLabel = UILabel()
    var questionView = UIView()

    // METHODS:

    func createQuestion (input:String) {

        // some code .... not important

        // THIS:
        self.view.addSubview(questionView)

    }

    // ... next code, also not important
}

UPDATE:

There is my solution. It works BUT I think that it's not correct from a programming standpoint. Can anybody tell me anything about it? Thank you

My class in separate swift file:

My class in separate swift file:

class LabelClass {

    var view = UIView()

    init (view: UIView) {
        self.view = view
    }

    var lbl = UILabel()
    var lblView = UIView()

    func makeLabel () {

        self.lbl.frame = CGRectMake(0, 0, 150, 50)
        self.lbl.text = "Text text text"
        self.lbl.numberOfLines = 0

        self.lblView.frame = CGRectMake(20, 20, 150, 50)

        self.lblView.addSubview(self.lbl)
        self.view.addSubview(lblView)

    }

}

Piece of code my ViewController.swift:

override func viewDidLoad() {
    super.viewDidLoad()

    // Added code:
    var object = LabelClass(view: self.view)
    object.makeLabel()

}

Upvotes: 0

Views: 441

Answers (2)

Özgür Ersil
Özgür Ersil

Reputation: 7013

It is because you are trying to add your view to a normal Swift class which doesn't have a self.view instance. Your Question class must be a subclass of UIViewController cocoa class that it has a self.view instance and override methods.

class Question:UIViewController {

    // PROPHERITIES:

    var questionLabel = UILabel()
    var questionView = UIView()

    // METHODS:
    override func viewDidLoad() {
         createQuestion("foo")
    }

    func createQuestion (input:String) {

        // some code .... not important

        // THIS:
        self.view.addSubview(questionView)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // ... next code, also not important
}

Upvotes: 1

oarfish
oarfish

Reputation: 4622

I don't know Swift, but as far as I know, only instances of UIViewController have a view property, the class Question does not, so you cannot add subviews to it. What you probably want is making a subclass of UIView which contains a question label, or to add the questionLabel as a subview of questionView.

Upvotes: 1

Related Questions