Lukas Köhl
Lukas Köhl

Reputation: 1579

Adding an UIImageView into the Subview

I have a function which is meant to be adding things into the Subview.

    func addView(object: UIImageView){
        view.addSubview(object)
    }

In another class I got a function, that want to use the add function.

    var field = UIImage(named: "Picture.png")
    var field1 = UIImageView(frame: CGRectMake(fieldX, fieldY, fieldWidht, feldLength))
    field1.image = field
    addView(field1)

The problem I have is saying:

Use of unsolve identifier 'addView'

in the same line where I try to get the access on this function: (addView(field1)).

Upvotes: 0

Views: 62

Answers (2)

Caio
Caio

Reputation: 370

You don't have the first class instance. You need first to init the class, and then you can call the addView: function.

Upvotes: 0

Lamour
Lamour

Reputation: 3030

Because you are calling this function from another class, you need to create an instance of that class, let's say the addView function was into a class called MainClassViewController

let targetController = MainClassViewController()

Then in your code where you want to call that function you do the following:

 let field = UIImage(named: "Picture.png")
 let field1 = UIImageView(frame: CGRectMake(fieldX, fieldY, fieldWidht, feldLength))
 field1.image = field
 targetController.addView(field1)  //<-- this is how you call that function

Upvotes: 1

Related Questions