1

I want to populate a UITableView with a list of paired Bluetooth devices but I'm unsure how to obtain the view object so I can populate it. Under Android each UI element is assigned a resource ID which is used to get an instance of a particular UI element but I'm not sure how this is done in iOS.

In my ViewController class I have this:

@IBOutlet weak var btListView: UITableView!

I'm looking to assign this variable to the Tableview instance on my main storyboard presumably within viewDidLoad().

Also, when I try to assign an outlet to the view by right clicking it in the storyboard the + options are there but nothing happens when I click New Referencing Outlet.

thedp
  • 8,350
  • 16
  • 53
  • 95
glez
  • 1,170
  • 3
  • 16
  • 42
  • 1
    If the view controller is a `UITableViewController` then the connection to self.tableView will already exist. If you're doing a custom `UIViewController` then you can control-drag from the table view to the outlet to make the connection. – David S. May 24 '18 at 14:57
  • I would say that even just answering this question won’t help you too much. I would suggest going more basic and looking up simple tutorials for how to use Storyboards, `@IBOutlet`s and `UITableView`s. – ABeard89 May 24 '18 at 15:04
  • Not that we couldn’t or don’t want to help. I just feel like you’d learn more and do so more quickly than by the back and forth here. – ABeard89 May 24 '18 at 15:05
  • Apparently using the + new outlet doesn't work in XCode; control drag did the trick although I still don't see where the connection is made and assume its under the covers. Thanks for the help...ed – glez May 24 '18 at 15:16
  • 1
    You need to look into the `UITableViewDataSource` methods to which your view controller should be conforming. Specifically in `tableView(_:, cellForRowAt:)` where you create the cell, that's where you set it up to display the content for a given index path, which you use to get the data that it's representing. – Guy Kogus May 24 '18 at 15:19
  • It looks like I picked up on the wrong tutorial as it designates extensions to the ViewControler to control the data source. Prior to adding the outlet I was at least getting to code that tried to load the list but now the code isn't reached. Time to go back to basics. – glez May 24 '18 at 15:54

1 Answers1

1

I'm looking to assign this variable to the Tableview instance on my main storyboard presumably within viewDidLoad().

You don't have to do anything in your code. Just connect the table view to the btListView outlet in your storyboard, and you'll find that when viewDidLoad() executes, btListView will already refer to the table. That's really the nice thing about outlets... you connect them when you design your interface, and that connection is re-created for you when the objects in the storyboard scene are loaded.

To make the connection in the storyboard, find the relevant scene in your storyboard and look at the list of objects in it. Your view controller should be among those, and you can examine its list of outlets by control-clicking on the view controller. A list will pop up, and you can either drag from the little circle at the right of the btListView outlet to the table, or you can control-drag from the table to the view controller and select the btListView outlet when it pops up.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Works. I'm also interested in where/how XCode, Swift or the core libs actually associate the UI element with the Swift outlet variable. The best I can tell is there is code in an .xib file somewhere in the project file system – glez May 25 '18 at 12:20
  • No, there's no code in the .xib. Very briefly, objects in a .xib or storyboard scene are all instances of classes that conform to NSCoding, so the .xib/storyboard editor essentially serializes the graph of objects. When you call UIStoryboard's `init(name:bundle:)` method or load a view controller from a .xib file, the objects are deserialized and the relationships between them are restored. – Caleb May 25 '18 at 13:12
  • Interesting. Where are the relationships stored? – glez May 25 '18 at 16:47
  • 1
    In the same file, along with everything else. You can open a storyboard with a text editor and look at it if you want. Multiple references to the same object are handled by NSKeyedArchiver/Unarchiver. Read the docs on those classes and NSCoding — it's powerful stuff and very useful to know about. – Caleb May 25 '18 at 17:11
  • That's what I thought but the storyboard always opened in XCode as a visual. I can see the linkage now with a text editor. Thanks for the guidance. – glez May 26 '18 at 12:54