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
Objectat the same level of theUITableViewControllerin the Storyboard (You cannot put theObjectwithin anotherUIViewController).
Link it with an@property (nonatomic, weak) IBOutlet NSObject *in theUITableViewController.
TheNSObject *turns out to be anilpointer when I print it inviewDidLoad. No luck. - By changing the property to
@property (nonatomic, strong), it become aUICustomObjectinstead ofnil. Seems I’m on the right track.
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:
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
strongfor the IBOutlet pointing to your custom object, because it’s a top level object and top level objects should be referenced usingstrongproperties. (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.)