-3

I'm trying to understand how to call from another class for example let creation = menuButtonController(image:#imageLiteral(resourceName: "maps")) then in menuButtonControlleri set this:

class menuButtonController: UIView {

    let buttonView = UIView()
    let button = UIButton(type: .custom)
    var imageButton: UIImageView


    init(frame: CGRect, image: UIImage) {
        super.init(frame: frame,image: image) //Cannot invoke  UIView.init with an argument list of type (frame cgrect, image uiimage)
        self.imageButton.image = image
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /* other functions */
}

it give me a error that i'm not able to solve. can anyone helps me?

Edit

now if i want to call only initializer with (image: UIImage) should i use a convenience init like this right?

convenience init(image: UIImage) {
    self.init(image: image)
    self.imageButton.image = image
}

it don't give me errors but crash when i try to run the application

Fiorelo Odobashi
  • 121
  • 1
  • 1
  • 11
  • Can you share what's the error that you get? – badhanganesh May 25 '17 at 20:56
  • i write it in the comment inside the code. here's a copy: Cannot invoke UIView.init with an argument list of type (frame cgrect, image uiimage) – Fiorelo Odobashi May 25 '17 at 20:57
  • 1
    Naming a subclass of `UIView` with a name of the form `xxxController` is a **terrible** idea. A view object and a controller object are totally different animals, and an object named `xxxController` should be a controller object, not a view object. Oh, and class names should start with an upper-case letter. So your class should be named something like `MenuButtonView` – Duncan C May 25 '17 at 21:05

1 Answers1

1

There is no method init(frame: CGRect, image: UIImage) in the super class UIView. You need to initialize imageButton first and call the designated initializer init(frame:

init(frame: CGRect, image: UIImage) {
    self.imageButton.image = image
    super.init(frame: frame)
}

By the way: Class names are supposed to start with capital letter.

vadian
  • 274,689
  • 30
  • 353
  • 361