What can the "Object" in Storyboard's Object Library do?
Asked Answered
H

2

12

Short Question

There is a thing called Object in XCode's interface builder's Object Library. I wonder what kind of task can be achieved by using this "Object".

Be specific, it is an NSObject in Storyboard (or xib). It's described as follow:

Provides a template for objects that are not directly available in Interface Builder. You can turn this object into an instance of any class using the custom class inspector.

Long Question

I'm trying to build a UITableViewController along with a UISearchController. Since they both require implement the UITableViewDelegate and UITableViewDatasource in their delegate, I wish I can assign the delegate of UISearchController to an Object other than the Main UITableViewController.

I'm looking for a way to achieve this in Storyboard, rather than assign the delegate manually in viewDidLoad. Especially, by figuring out how the Object work in Storyboard.

I understand that I can distinguish them by exam the UITableView's parents. But it is always good to separate the logic into different objects. It will be easier to implement, better efficiency, better structured and possible to be reused.

Thanks in advance.

What I have tried

  • Put an Object at the same level of the UITableViewController in the Storyboard (You cannot put the Object within another UIViewController).
    Link it with an @property (nonatomic, weak) IBOutlet NSObject * in the UITableViewController.
    The NSObject * turns out to be a nil pointer when I print it in viewDidLoad. No luck.
  • By changing the property to @property (nonatomic, strong), it become a UICustomObject instead of nil. Seems I'm on the right track.
Heiskell answered 22/7, 2012 at 1:23 Comment(2)
Object is used to provide an Interface Builder link for your custom classes and their inputs and outlets.Ming
I think I'm getting it. Trying to play around. Thanks for the comment.Heiskell
G
9

As you note, Object makes it so an object of any class, including your own custom class ,is instantiated when the XIB/Storyboard is loaded. The fact that you're getting a UICustomObject indicates that the XIB/Storyboard loading machinery either doesn't know what class the object should be, or can't find the class (via runtime lookup) you've specified.

The solution is to make sure you specify the right class for the Object in the custom class inspector:

enter image description here

Then of course you also need to make sure that the implementation file for the class in question is in your project and is included in your target.

Finally, it's correct to use strong for the IBOutlet pointing to your custom object, because it's a top level object and top level objects should be referenced using strong properties. (If you're really curious, Mike Ash has a good article about the specifics of XIB/Storyboard outlet memory management, including the differences between OS X and iOS in this regard.)

Gallia answered 22/7, 2012 at 3:32 Comment(2)
Thank you for this good explanation. Besides these, I also discovered some interesting facts. 1. Since this object is created by Storyboard at runtime, it's not possible to specify any argument for the initializer. 2. The object itself cannot be connected to a segue or invokes one, it has to call the main view controller.Heiskell
Dear @AndrewMadsen, you may possible know the answer to this one .. https://mcmap.net/q/1009931/-storyboard-drag-link-to-an-ibaction-from-a-container-view-to-the-parent/294884 ... I thin it relates to these mysterious "Object" objects!! cheers ...Scorper
O
5

This question has been marked as answered, but since it is an open question for which there can be multiple answers, there is a clever technique for doing as you say - breaking up functionality into single role classes that the author refers to as "Behaviors":

http://www.objc.io/issue-13/behaviors.html

The gist of it is that you can write an object that for example performs a parallax effect by exposing outlets to multiple views. It is set as the scroll view delegate and updates the other view based on configurable settings. As you noted, you cannot pass arguments to a constructor, but you can still specify parameters via the key path mechanism illustrated in the image in @Andrew's answer.

As you also noted, you can then run into the problem where you would would want to have different objects responding to scroll events etc. The author addressed this by coding a simple multiplexer. You can also solve this with the chain of responsibility pattern: http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern.

And finally, @Andrew mentioned in his answer the importance of storing the object as a strong property. They author of the aforementioned article - lets just call him "Krzysztof", provides a solution using the obj-c associated object mechanism to allow for configurability without code. Your mileage may very with this, and there may be better options now with Swift, but at the very least, it is some good food for thought.

Ockeghem answered 13/10, 2014 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.