-3

I have looked at the other similar questions but didn't find any solution for my code, could someone help me? Thank you!

Here is the code:

import UIKit

let therColor = UIColor.greenColor()
let rMin2 = CGFloat(Int(ViewController().sliderMinOutlet.value))
let rMax2 = CGFloat(Int(ViewController().sliderMaxOutlet.value))
let center2 = CGPoint(x:400, y:400)
let startAngle2 = CGFloat(-M_PI/3)
let endAngle2 = CGFloat(M_PI/3)
let point1contact2 = CGPoint(x:center2.x+rMin2*cos(-startAngle2), y:center2.y-rMin2*sin(-startAngle2))
let point2contact2 = CGPoint(x:center2.x+rMax2*cos(-startAngle2), y:center2.y-rMin2*sin(-startAngle2))

class drawArcs: UIView {
override func drawRect(rect: CGRect) {
    let contact2Ther = UIBezierPath()
    contact2Ther.moveToPoint(point1contact2)
    contact2Ther.addArcWithCenter(center2, radius: rMin2, startAngle: startAngle2, endAngle: endAngle2, clockwise: true)
    contact2Ther.moveToPoint(point2contact2)
    contact2Ther.addArcWithCenter(center2, radius: rMax2, startAngle: startAngle2, endAngle: endAngle2, clockwise: true)
    contact2Ther.closePath()
    therColor.setFill()
    contact2Ther.fill()
}
}

And here the ViewController:

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var sliderMinOutlet: UISlider!
@IBOutlet weak var sliderMaxOutlet: UISlider!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

This is the code in the ViewController: ViewController code

And this is the code in the UIView class:

UIView class code with error when built

Fabio Laghi
  • 197
  • 1
  • 2
  • 6
  • @EricD, it's not really a duplicate because the actual reason is different. – vadian Jun 14 '16 at 09:15
  • @vadian Given the screenshots, I thought their IBOutlet was nil. Don't you think? I'll reopen if you think their issue is not listed in the target. – Eric Aya Jun 14 '16 at 09:19
  • 1
    @EricD The reason are the two different generic `ViewController` instances which are of course not that one in the storyboard. – vadian Jun 14 '16 at 09:24
  • Did you override the `init ` method to load from a `nib`? If not, you're initializing ViewController without `init(nibName...`, then the IBOutlets are not connected and is nil, cause the crash – Duy Pham Jun 14 '16 at 09:24
  • @vadian Arf, yes, just seen that. I've reopened. Thanks for noticing. – Eric Aya Jun 14 '16 at 09:25
  • @DuyPham I didn't do it, because I don't know how. Can you point me in the right direction? thank you! – Fabio Laghi Jun 14 '16 at 16:09

1 Answers1

0

If you're not initializing the UIViewController from a nib that has those 2 IBOutlets connected, then those 2 will certainly be nil, and cause crashes since they're declared as explicitly unwrapped optionals.
You need to call init(nibName:bundle:) with a correct nib.

Duy Pham
  • 191
  • 3