How to create a button through coding not by UI in Mac OS Application using swift 4.

Hi there, i am trying to add buttons into the UI by using the swift code so that i can add multiple buttons dynamicallywith custom actions.

I have a custom box and i want to add buttons inside the box when my function executes.


I am using Xcode 9.3 and swift 4.

Accepted Reply

Yes.


        let button = NSButton(frame: NSRect(x: 150, y: 200, width: 80, height: 55))
        button.title =  "A button in code"
        window!.contentView?.addSubview(button)
        button.target = self
        button.action = #selector(buttonTest)


Create the action as :


    @objc func buttonTest() {
        print("Test button")
    }

Replies

Yes.


        let button = NSButton(frame: NSRect(x: 150, y: 200, width: 80, height: 55))
        button.title =  "A button in code"
        window!.contentView?.addSubview(button)
        button.target = self
        button.action = #selector(buttonTest)


Create the action as :


    @objc func buttonTest() {
        print("Test button")
    }

Okay, Now I have a table view and i want to add this button into the table view as a cell.


I am trying like below

-------------------------

outlet of table view:

@IBOutlet weak var TableView: NSScrollView!


button click event:

@IBAction func DemoBtn(_ sender: Any) {
        let button = NSButton(frame: NSRect(x: 150, y: 200, width: 80, height: 55))
        button.title =  "A button in code"
        window!.contentView?.addSubview(button)
        // I want to add this button into the table view, every time i click it should add a new button.
    }


How to set the event for each button generated inside the table view ?


Can you tell me how to do it please.

You should better open a new thread when you ask a new question.


You want a button in each cell ?

I suppose you create the TableView in IB.

Connect to an IBOutlet:

    @IBOutlet weak var simpleTableView: NSTableView!


Then, in IB Identity Inspector, give an identifier to its Table Cell View "CellIdentifier"


Make the class that contains the TableView conform to NSTableViewDelegate, NSTableViewDataSource

At the load (could be appDidFinisLaunching), set the delegates


        simpleTableView.delegate = self
        simpleTableView.dataSource = self


Define the delegate and dataSource func.

Create the button in tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int)



    // MARK:- TableView Delegate

    func numberOfRows(in tableView: NSTableView) -> Int {
  
        return 3     // The number of rows you have
    }

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

            if let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "CellIdentifier"), owner: self) {
                let button = NSButton(frame: NSRect(x: 10, y: 2, width: 80, height: 20))     // Take care that it fits in the cell
                button.title =  "Action"
                button.target = self // The class where tableView is declared
                button.action = #selector(buttonTest)
                button.tag = row          // That will allow to find which cell the button is in
               (cellView as! NSTableCellView).textField?.stringValue = "Cell \(row + 1)"
                cellView.addSubview(button)
                return cellView
            } else {
                return nil
            }
    }

Complete the action to see the tag, and use it

    @objc func buttonTest(_ sender: NSButton) {
        print("Test button", sender.tag)
       swicth sender.tag {
          case 0: print("First cell clicked")
          case 1: print("Second cell clicked")
          case 2: print("Third cell clicked")
          default : break
     }
    }