Reputation: 1818
If I create an object in paint code I, am then either given a style sheet or code from the tab section.
Stylesheet:
import Cocoa
public class StyleKitName : NSObject {
//// Drawing Methods
public class func drawMain() {
//// Color Declarations
let color = NSColor(calibratedRed: 1, green: 0, blue: 0, alpha: 1)
//// Rectangle Drawing
let rectanglePath = NSBezierPath(rect: NSMakeRect(29, 30, 198, 166))
color.setFill()
rectanglePath.fill()
}
}
Code from tab:
//// Color Declarations
let color = NSColor(calibratedRed: 1, green: 0, blue: 0, alpha: 1)
//// Rectangle Drawing
let rectanglePath = NSBezierPath(rect: NSMakeRect(29, 30, 198, 166))
color.setFill()
rectanglePath.fill()
How do I then go about importing this into my swift project. I have tried putting the style sheet in a separate .swift
file then calling the function from my viewDidLoad
function in the NSViewController
by doing: var rectangle = StyleKitName.drawMain()
but nothing appeared on the NSView. I also tried pasting the code from the tab directly into the view controller's view did load but nothing appeared on the view controller.
Does anybody know what I'm doing wrong?
Upvotes: 0
Views: 154
Reputation: 8718
You're a bit lost in the woods with regard to creating a custom view.
1) StyleKitName must inherit from NSView
. Read the NSView class reference.
2) To define how a custom view paints itself, you must override drawRect()
, not define your own function.
3) To cause a view to paint itself, you must not call drawRect()
directly, but instead call setNeedsDisplayInRect()
4) viewDidLoad() is not the proper place in the ViewController lifecycle to be calling drawing functions. If instead you layout your custom view within your main view by using addSubView()
within viewDidLoad()
, you won't need to initiate any drawing requests. You're supposed to do the layout at load time, define how a view should be drawn at compile time, and let OS X decide when to redraw it at run time.
Upvotes: 1