0

I want to programatically create an image view in a function. This will hold the background of the image view and some logos - then in all my other image views I can just all the function and I wan't have to do the same thing on each one.

I created a blank swift file and put the following in it:

import Foundation
import UIKit

func background() {

    var imageView : UIImageView
    imageView  = UIImageView(frame:CGRectMake(10, 50, 100, 300));
    imageView.image = UIImage(named:"image.jpg")
    self.view.addSubview(imageView)

}

The only error I got was on the self.view.addSubview(imageView) line. The error message was Use of unresolved identifier 'self'. I tried just removing the word self so the line said this instead view.addSubview(imageView) but then Use of unresolved identifier 'view'.

My question is what am I doing wrong?

Thanks

EDIT:

So I have changed it to this now, is this correct?

import Foundation import UIKit

class MyViewController: UIViewController {

    func background() {

        var imageView : UIImageView
        imageView  = UIImageView(frame:CGRectMake(10, 50, 100, 300));
        imageView.image = UIImage(named:"bg.png")
        self.view.addSubview(imageView)

    }

}

I no longer get any errors in that part but when calling the function from the viewcontroller class I get the error message Use of unresolved identifier 'background'. I should add that I am calling it in the other document by doing thisbackground()` in the view did load function.

6
  • How are you calling background? Commented Apr 1, 2015 at 15:01
  • I've tried calling it doing background() which gave me the error message Use of unresolved identifier 'background' and I also tried calling it using MyViewController.background() and got the error message Missing parameter for #1 in call. Commented Apr 1, 2015 at 15:04
  • background() is an instance method which means it can all be called on an instance of your class. MyViewController.background() thinks it's a class method. Commented Apr 1, 2015 at 15:06
  • @aahrens So to make sure I get this right, I am calling it wrong? I should be calling it as an instance of the class - the way I am calling it thinks it is a class method? Would it be better if it was a class method? I'm a bit confused because both of those ways didn't work. Commented Apr 1, 2015 at 15:10
  • You could do it either way Commented Apr 1, 2015 at 15:12

2 Answers 2

2

To call an instance function in swift you can do it two ways, one from within another instance function or by creating a instance of the class and then calling it.

From an instance function:

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Called from within another instance function
        self.background()
    }

    func background() {

        var imageView : UIImageView
        imageView  = UIImageView(frame:CGRectMake(10, 50, 100, 300));
        imageView.image = UIImage(named:"bg.png")
        self.view.addSubview(imageView)

    }

}

Or somewhere else in code you can create an instance and call background like this:

func someOtherFunction() {
// Create instance and then call background
        var myView:MyViewController = MyViewController()
        myView.background()
    }

I would recommend you learn the difference between a instance method vs a class method and decide what you are trying to do: What is the difference between class and instance methods?

Sign up to request clarification or add additional context in comments.

Comments

0

You need a class that is a subclass of UIViewController in order to have access to the view property

2 Comments

Thanks for your answer. I have updates my question with the issue this is not giving me.
I would change your class name to be more descriptive. Like class MyViewController : UIViewController

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.