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.

cross
  • 363
  • 1
  • 4
  • 15
  • How are you calling background? – aahrens Apr 01 '15 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`. – cross Apr 01 '15 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. – aahrens Apr 01 '15 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. – cross Apr 01 '15 at 15:10
  • You could do it either way – aahrens Apr 01 '15 at 15:12
  • @aahrens So could if I left the code the same and I just wanted to call it, would just doing `MyViewContoller()` work? I tried this but it doesn't work - I don't really want to ask you for the actual answer because then its like you've coded it for me but any hints as to where to look on info on how to call it would be much appreciated. – cross Apr 01 '15 at 15:20

2 Answers2

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?

Community
  • 1
  • 1
Michael Wildermuth
  • 5,762
  • 3
  • 29
  • 48
0

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

aahrens
  • 5,522
  • 7
  • 39
  • 63
  • Thanks for your answer. I have updates my question with the issue this is not giving me. – cross Apr 01 '15 at 14:58
  • I would change your class name to be more descriptive. Like class MyViewController : UIViewController – aahrens Apr 01 '15 at 15:00