0

I'm trying to pass the contents of an array to an NSView to display rectangles x positions. I have set up a version using random numbers and a button and reduced the code to a minimum so hopefully it's more readable . I have tried creating the array as a Global Variable. I have also tried reversing the code from this answer.Pass data between view controllers.I have inserted a comment: "This is what I would like to achieve" which explain what i'm trying to achieve. I'm hoping someone might help ? thanks

Swift 3 Xcode 8.1 OSX Mac OS not iOS

//
//  MainWindowController.swift

import Cocoa
class MainWindowController: NSWindowController {
    @IBOutlet weak var myView: NSView!
    var myArray = [Int]()
    override func windowDidLoad() {
        super.windowDidLoad()
        //just for testing
        for i in 0..<6{
            myArray.append(i*10)
        }
    }//EO Overide

    //just for testing
    @IBAction func myButton(_ sender: AnyObject) {
        myFunc()
    }

    func myFunc(){
        for i in 0..<6{
            let diceRoll = Int(arc4random_uniform(6) + 1)
            myArray[i]=diceRoll * 10
        }

        myView.needsDisplay = true

    }//EO myFunc
}//EnD oF thE wORld



class myGUI: NSView {

    override func draw(_ dirtyRect: NSRect) {

        for i in..<6{
   let fromArray = myArray[i]
            let box1 = NSMakeRect(CGFloat(fromArray),0,4,60)//This is what i want to do
            let box1Color = NSColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 1.0)
            let box1Path: NSBezierPath = NSBezierPath(rect: box1)
            box1Color.set()
            box1Path.fill()

        }//EO For

    }}
Community
  • 1
  • 1
oldman
  • 145
  • 1
  • 15

1 Answers1

0

Since you have a strong reference to the subclass of NSView just add a property:

class myGUI: NSView {

    var xPosArray = [Int]()      

    override func draw(_ dirtyRect: NSRect) {
        for xPos  in  xPosArray { // please avoid ugly C-style index based loops
           let box1 = NSMakeRect(CGFloat(xPos), 0, 4, 60)
    ...

and simply set it in the view controller

func myFunc(){
    for i in 0..<6 {
        let diceRoll = Int(arc4random_uniform(6) + 1)
        myArray[i]=diceRoll * 10
    }
    myView.xPosArray = myArray
    myView.needsDisplay = true
 }

Consider that class names are supposed to start with an uppercase letter.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • I take note about class names and ugly loops I never knew that loop style existed (self taught) I am getting the error. "Value of type 'NSView' has no member 'array' " Thanks – oldman Mar 18 '17 at 10:37
  • In Interface Builder you have to set the class of the custom view to `myGUI` – vadian Mar 18 '17 at 10:54
  • I had already set the class of the view to myGUI still no luck – oldman Mar 18 '17 at 10:58
  • I know that part is working have tested it using a global variable set from myFunc – oldman Mar 18 '17 at 11:04
  • Additionally you have to change this line: `@IBOutlet weak var myView: myGUI!`. A global variable is not recommended and not needed at all in this case. – vadian Mar 18 '17 at 11:21
  • I was only using a global variable to test the GUI was working. which it is .I have reposted the code with your additions but still get the Error as commented. – oldman Mar 18 '17 at 11:43
  • Yes you are correct but that line only worked when i changed all the other things you said. Great Answers – oldman Mar 18 '17 at 12:04
  • I have updated the original code so that it works tried to + this answer is useful but i don't have enough reputations – oldman Mar 18 '17 at 12:09
  • Please don't edit the question with the correct code, so nobody can follow the step from the issue to the solution. Probably you have enough reputation to accept the answer. – vadian Mar 18 '17 at 12:11
  • sorry changed it back to original – oldman Mar 18 '17 at 12:39