1

I've built a custom button and now I'm trying to put it in a class.

The code below throws an error for the line "self.view.addSubview(button)" - which is what I used when the code was running under viewDidLoad.

Any suggestions?

 class myButton {
    var buttonLabel:String
    var buttonRadius:CGFloat = 90
    var button = UIButton.buttonWithType(UIButtonType.System) as UIButton

    init(buttonColour:CGColorRef, buttonLabel:String){
        self.buttonColour = buttonColour
        self.buttonLabel = buttonLabel
    }

    func drawButton() {
        button.frame = 100, 100, buttonRadius * 2, buttonRadius * 2)
        button.layer.borderColor = buttonColour
        button.setTitle(buttonLabel, forState: UIControlState.Normal)
        button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(button) // error: 'ViewController.myButton' does not have a member named 'view'
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
gemello
  • 393
  • 1
  • 4
  • 8

1 Answers1

1

You are doing it wrong. Your class does not have view property. I guess you were trying to use view controller. So every viewcontroller has a property named view of type UIView which is what you are looking for.

class MyViewController: UIViewController {
  var buttonLabel:String
  var buttonRadius:CGFloat = 90
  var button = UIButton.buttonWithType(UIButtonType.System) as UIButton

  init(buttonColour:CGColorRef, buttonLabel:String){
    self.buttonColour = buttonColour
    self.buttonLabel = buttonLabel
  }

  func drawButton() {
    button.frame = 100, 100, buttonRadius * 2, buttonRadius * 2)
    button.layer.borderColor = buttonColour
    button.setTitle(buttonLabel, forState: UIControlState.Normal)
    button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button) // error: 'ViewController.myButton' does not have a member named 'view'
  }
}
Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • 1
    Thanks for quick response! /n – gemello Sep 21 '14 at 13:45
  • @gemello if this answer solved your issue please consider accepting it. It'll help you and future users who face this problem. – Benjamin Gruenbaum Sep 21 '14 at 13:46
  • 1
    @BenjaminGruenbaum - I've just tried the change, but now I'm getting another error - fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class. – gemello Sep 21 '14 at 13:56
  • What are you actually trying to do with this class that you're inheriting from UIViewController? Are you sure you don't mean to subclass UIView and implement drawRect for custom drawing logic? Please be more specific about your _goal_ – Benjamin Gruenbaum Sep 21 '14 at 13:58
  • My goal is to create a button programmatically - I got it working fine within viewDidLoad, but can't figure out how to make it work as a class. – gemello Sep 21 '14 at 14:03
  • @gemello why do you have a drawButton then? Wouldn't it be easier to draw it once and set its `hidden` property to true when yo uwant to show it? That way you could even make it in the interface builder. – Benjamin Gruenbaum Sep 21 '14 at 14:42
  • Yep - I used a drawButton to call the button class in Obj-C - so yes, trying to figure out how to achieve the same goal in Swift (create a button class file that I can easily add to different projects, rather than pasting code. – gemello Sep 21 '14 at 14:53