Emile
Emile

Reputation: 41

How to add views while using a function in a separate class in Swift?

I created a new Swift-file with a class which holds a function which should add some UILabels, UITextFields and an UIButton to the viewcontroller. When this code is in the ViewController it works, but in the separate file I get some error messages:

class InitialiseLabels {
    func setupLabels() {

        // multiple variables are declared without problems, but then:
        let viewWidth = view.frame.width // Use of unresolved identifier 'view'
        let viewHeight = view.frame.height // Use of unresolved identifier 'view'
        view.addSubview(myLabel) // Use of unresolved identifier 'view'

        // etcetera

        myTextField.delegate = self as" UITextFieldDelegate // Use of unresolved identifier 'self'
        view.addSubview(myTextField) // Use of unresolved identifier 'view'
    }
}

I guess that I should add something in front of view to specify where it should go (like self.view but that one obviously doesn't work, the name of this ViewController also doesn't work). Can somebody please help me?

Upvotes: 0

Views: 1249

Answers (5)

Emile
Emile

Reputation: 41

I found out what I was doing wrong: I put the declaration of the function in the viewdidload and because of that it was not available in the other parts of the view controller. Because of that I wanted to put it in a separate class or extension but I realised now that the solution is to put the function declaration right after the view controller class declaration.

Upvotes: 0

matt
matt

Reputation: 534950

Change

class InitialiseLabels

To

extension ViewController

Even if this is in another file, it has to be part of the original ViewController class, not some other class.

Upvotes: 0

iOSer
iOSer

Reputation: 2326

Maybe create a singleton of the swift file and then refer to the singleton while calling the functions that add the UILabel, UIButton etc. to your current view controller

Suppose following is the swift file

class ButtonCreator : NSObject {
   //Declare the Singleton
   static let shared = buttonCreator()

   func thisFunctionCreatesandReturnAButton(withFrame: CGRect) -> UIButton {
       let button = UIButton(frame: withFrame)
       return button 
   }
}

Now you can refer it throughout your application (any view controller) as

self.view.addSubview(ButtonCreator.shared.thisFunctionCreatesandReturnAButton(withFrame:CGRect(0,0,100,44)))

Upvotes: 0

Duncan C
Duncan C

Reputation: 131408

The short answer to your question "How to add views while using a function in a separate class in Swift?" is "Don't do that."

You should treat a view controller's views as private. The view controller should be responsible for them.

If you really wanted to let an outside object add views to your view controller (again, you shouldn't do that) you'd need a reference to the other view controller. It would be cleaner to create a function that would take the new views as parameters and add them for the caller:

public func addViewdToContent(_ views: [UIView]) 

Then from the other object, you'd need a reference to that view controller:

let otherViewConroller = //code to somehow get the other view controller. 

let contentView = otherViewConroller.view
let viewWidth = contentView.frame.width
let viewHeight = view.frame.height
otherViewConroller.addViewToContent([myLabel]

Your next line doesn't really make sense, and neither does the error you're getting. It is a very bad idea to make some other object the delegate of a view controller's views. It is also a bad idea to inject views like text fields that need delegates into another view controller.

myTextField.delegate = self as" UITextFieldDelegate // makes no sense

Upvotes: 2

Satish
Satish

Reputation: 2043

Create a class MyView and assign that to VC's Main View. And put your code inside that class.

class MyView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

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

    override func layoutSubviews() {
        super.layoutSubviews()

        // add code to align view properly
    }

    private setup() {
        ....

        let viewWidth = frame.width // Use of unresolved identifier 'view'
        let viewHeight = frame.height // Use of unresolved identifier 'view'
        addSubview(myLabel)

        myTextField.delegate = self as" UITextFieldDelegate // Use of unresolved identifier 'self'
        addSubview(myTextField) /

        ...
    }
}

Upvotes: 0

Related Questions