# How to add a button to the viewcontroller? swift

**URL:** <https://forums.swift.org/t/how-to-add-a-button-to-the-viewcontroller-swift/65103>\
**Category:** Using Swift\
**Tags:** swift, xcode, macos\
**Created:** [May 21, 2023, 10:55am UTC](https://forums.swift.org/t/how-to-add-a-button-to-the-viewcontroller-swift/65103 "2023-05-21T10:55:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pretty\_Girl](https://avatars.discourse-cdn.com/v4/letter/p/ecc23a/32.png) [@Pretty\_Girl](https://forums.swift.org/u/Pretty_Girl)\
**Post date:** [May 21, 2023, 10:55am UTC](https://forums.swift.org/t/how-to-add-a-button-to-the-viewcontroller-swift/65103/1 "2023-05-21T10:55:04Z")

</div>

The following code outputs an empty user interface. When I go to `main` in xcode and I were able to add a button manually. Then it shows the button. But I want to write code to add a button and I don't want to drag and drop components. How to insert (by typing) a button into the following code? I can see there is a button and a textfield, if those should be visible in runtime, let me know why they're not showing up?

```swift
import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var label: NSTextField!
    @IBOutlet weak var button: NSButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    @IBAction func buttonClicked(_ sender: Any) {
        label.stringValue = "Button clicked!"
    }

}

```
