matcartmill
matcartmill

Reputation: 1593

iOS Swift - Accessing method in Child from Parent

In my app I have a Storyboard with a bunch of elements laid out. I am setting properties of these elements from "ViewController.swift".

On the storyboard are two UIViews, which have been subclassed to allow for drawing methods. They are to be used as "signature" fields, where a user can sign their signature into the box. This is working fine.

One of the subclassed methohds of the UIViews is "eraseBitmap" which clears the UIView of the drawing.

class SigView: UIView {
    ...
    func eraseBitmap() {
        path = nil
        cache = nil
    }
}

I have a button that calls a function "erase" in the parent VC.

I have tried doing

func erase() {   
    SigView.eraseBitmap()
}

However that generates an error saying that I'm missing an argument. eraseBitmap, however, accepts no arguments.

If I add an argument, regardless what it is, I get a "SigView not convertible to..." error.

Any ideas, or a better way of coding this part?

Upvotes: 0

Views: 2268

Answers (1)

GoZoner
GoZoner

Reputation: 70135

Your SigView class defines a method eraseBitmap() - something like:

class SigView : UIView {
  func eraseBitmap () { /* ... */ }
}

You'll apply this method to instances of SigView. So, somewhere you've got an instance of SigView in your UI, like:

var aView : SigView = ...

You'll then use:

func erase () {
  aView.eraseBitmap()
}

or perhaps

func erase (view:SigView) {
  view.eraseBitmap()
}

The error you are getting is caused by attempting to invoke a non-class method on a class. Non-class methods can only be invoked on instances of classes.

Upvotes: 1

Related Questions