Creating and Deleting Items

New items can be added programmatically to the diagram by calling the constructors of the item’s class, such as ShapeNode or DiagramLink, and adding the new instance via the addItem method. A shortcut to creating new items is provided by the methods of the Factory class. An instance of that class is returned by the factory property of Diagram. Factory methods such as createShapeNode and createDiagramLink accept respectively node position or link’s origin and destination arguments, create an instance of the class and add it to the diagram. The Factory class can create instances of SwiftDiagram’s predefined item types, while addItem can be used to add instances of custom types as well.


var dview:DiagramView!

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{
    dview = window?.rootViewController?.view as? DiagramView

    let diagram = dview.diagram;

    //create the start node
    let startNode = diagram.factory.createShapeNode(x: 10, y: 10, width: 30, height: 20)
    startNode.text = "start"

    //create the end node
    let endNode = ShapeNode()
    endNode.bounds = Rect(x: 10, y: 60, width: 30, height: 20)
    endNode.text = "end"
    diagram.addItem(endNode)

    //connect the nodes
    diagram.factory.createDiagramLink(origin: startNode, destination: endNode)

    return true
}

If an existing item should be removed from the diagram, call the removeItem method. If a node is removed, any links connected to the node are removed too.