Search code examples
swiftbuttonwatchkit

I have to add a list of button programmatically


How can I add some buttons programmatically in swift 2 using WatchKit?


Solution

  • You can't dynamically create views in WatchKit. You need to create your entire interface in a storyboard. You can have elements of your storyboard hidden and then programmatically unhide them.

    Reference : Create imageView programmatically in Watch Kit

    You can create table instead of the buttons. So, you can get multiple clickable links.

    Here is tutorial by @natashatherobot http://natashatherobot.com/watchkit-create-table/

    Snippet from the tutorial :

    private func loadTableData() {
        minionTable.setNumberOfRows(minions.count, withRowType: "MinionTableRowController")
        for (index, minionName) in enumerate(minions) {
            let row = minionTable.rowControllerAtIndex(index) as! MinionTableRowController
            row.interfaceLabel.setText(minionName)
            row.interfaceImage.setImage(UIImage(named: minionName))
        }
    }