How to add custom data source class using storyboard - ios

I can't find the way to add my custom data source class of UICollectionView in the storyboard. The data source class has an interface like this:
#interface CalendarDataSource : NSObject<UICollectionViewDataSource>
I try to find the calendar data source in the scene sidebar but I can't. So I can't seem to link the class to my UICollectionView. Is there something I should do but I didn't?
If you want to reproduce the similar problem, just create a new project in XCode5, set up a UICollectionViewController, and create a new class just like above.
I'm currently rewriting the project described in http://www.objc.io/issue-3/ The Github repo is https://github.com/objcio/issue-3-collection-view-layouts This project managed to do this, however. My code is almost the same to the project but can't run because viewcontroller.collectionView.dataSource is uninitialized.
Doing the initialisation programmatically is another issue, but please tell me in the comment because all I got from the internet is setting dataSource to self. But setting it to self doesn't require introducing a new retained data source object, which I got stuck on.

Go to the Interface Builder.
Right-click on your Collection View in the storyboard, delete dataSource outlet (if any).
Type "Object" in the search text field of the Objects Library and drag an 'Object' to your view controller containing UICollectionView (i.e. do the same as you add any view or view controller to storyboard scene, with the exception you add not a view or view controller but an abstract object).
In the left-side 'Scenes' panel of your storyboard, highlight just added Object; in right-side panel go to the 'Identity Inspector' and type CalendarDataSource instead of pre-defined NSObject. In left side panel ('Scenes'), 'Object' will be renamed to 'Calendar Data Source' automatically.
In the left-side 'Scenes' panel of your storyboard, control-drag from your UICollectionView to the 'Calendar Data Source'; in pop-up appeared, select dataSource outlet.
You've done!

Related

New Swift Application Creates Obj C Files

I am a bit new to iOS development. Am I going crazy here?
I am using xCode to create a paged application. I select new project with the language as swift.
When I go to add a new view to the application it adds a view with Objective C?? Why? I want to use Swift. Am I doing something wrong?
Adding more screenshots, how do I get the second view to show me the Swift code instead of the "UIKit" code?
Working Side...
Not working
3:
It looks like your Assistant Editor is automatically opening the underlying Objective-C header files rather than the Swift files associated with your View Controllers. Try clicking on Automatic at the top of the Assistant editor, then hovering for a moment to see if your Swift file comes up (it should appear above or below the UIViewController.h file).
If you haven't created a subclass for the View Controller you've created in the storyboard, you'll need to do that first, then assign that class to the View Controller in the storyboard. To do this, select File > New > Fileā€¦, select Cocoa Touch Class, and make it a subclass of UIViewController (don't create a XIB file). Then, in your storyboard, click on your newly-created View Controller (you can select it from within the view hierarchy on the left), open the Identity inspector, and set its Class to the subclass of UIViewController you just created.
If, for some reason, you've created a View Controller class and correctly associated it with the storyboard object and it's still not appearing, you can use the Manual selection in the Assistant editor to drill down to the file you want to appear.

Subclassing a View Controller

I have a single view aplicaition on xcode with several view controllers and xib files in it. I am trying to subclass a xib file's view controller called ViewControllerPg62 as a sub class of a view controller called ViewController. How can i do this?
Thanks in advance
Why are you working with XIB files? You should be using Storyboards.
I had to go back to an old project using XIBs to see how they worked.
In an XIB you should see an entry called "Placeholders", and under that, an item called "File's Owner." That represents the object that owns and manages the views in the XIB. Select that, and then display the Identity Inspector. There will be an entry at the top "Custom Class" where you can change the class that manages this view controller.
Before changing that, I suggest you create the source file for your view controller ("ViewController.swift", in your case.) Then when you change the class of the owning object, the XIB will create an instance of your custom class when that XIB is invoked.
The approach is quite similar for Storyboards, although it's a little more coherent.

trying to hook up UILabels and IBActions in a custom class (Xcode 5)

I am having issues making UILabels and IBAction buttons in custom classes. Xcode seems to not want me to do it. They way I'm trying to do it is through interface builder (storyboard). I have no issue clicking and dragging to make IBOutlets and IBActions using the main View Controller but when I click and drag over to connect them in a custom class it does nothing. Am I only able to make these in the View Controller? I've attached a pic of me trying to drag over to connect my button in the custom class and you can see that nothing pops up. So basically, Are labels, buttons, text fields etc, for the View Controller class only? Thanks for any feedback.
Within interface builder, you will need to make sure your view controller is using your custom class by opening utilities view on the RHS of the IB, selecting the third icon along (please see picture below) and enter your custom class name in the space provided.
Your custom class will need to be a subclass of UIViewController though, like the picture below, not an NSObject for example.
I'm referring to this line within your .h file ...
#interface MyCustomClassViewController : UIViewController
Your custom class needs to match the UI object you are connecting it with. For example if you had UIView object, your custom class would need to be a subclass of UIView, a UINavigationViewController object, a subclass of UINavigationController etc etc.
I hope this helps.
Select your view controller in the stoyboard and go to the identity inspector, make sure the name there is your custom class name.

can I create a .xib from an implementation file?

I have a project that has the header and the implementaiton files and loads a plain old UITableViewController from the .m but I have no way of configuring the View (I would like to do stuff like ad buttons and text fields onto the view instead of just having a table), should I create a .xib file so that I can modify the view? (Reason I don't just start a new project is that the current project has a bunch of other stuff like OAUTH processes that kick off at the AppDelegate stage to log a user into a system - and I need this functionality). So my question how can I create a .xib from the existing .m .h files and then add other objects which I can then code for.
An answer from this similar question:
"Here's a more step-by-step way to associate your new UIViewController and .xib.
Select File's Owner under Placeholders on the left pane of IB. In the Property Inspector(right pane of IB), select the third tab and edit "Class" under "Custom Class" to be the name of your new UIViewController subclass.
Then ctrl-click or right-click on File's Owner in the left pane and draw a line to your top level View in the Objects portion of the left pane. Select the 'view' outlet and you're done.
You should now be able to set up other outlets and actions. You're ready to instantiate your view controller in code and use initWithNibName and your nib name to load it."
If it's a UITableViewController, simply create a new XIB file via Xcode (File -> New -> iOS -> UserInterface -> View) and then add set the file's owner to your subclassed UITableViewController.
You'll likely want to re-do how the user interface looks -- in terms of dropping objects like the table view and buttons or whatever else -- into the XIB's view. It'll certainly save a lot of time versus trying to debug programmatically creating and adding subviews and actions.
And once that's in place, you can then make IBOutlets and IBActions to your heart's content.

iOS : Setting actions, datasources and delegates not to the file's owner

Maybe those are two different questions, but they basically ask the same thing.
I have a single view app, on my screen I have a Table View and a Collection View.
1) I want to provide a datasource (and delegate of course) for my tableview and for my collection view, but I don't want it to be in the same (main) ViewController file (in purpose of cleaner code). But the interface builder only allows me declaring as datasource (and delegate) the file's owner, which is the view controller. How can I do this? Is declaring programatically the right way? If so, where should it be done? Or, maybe, separating the datasources (and delegates) from the ViewController isn't the best practice?
2) In my table view's custom cells I have a button I wan't to deal with the button's actions in the same place (file) I deal with row selection (delegate obj). But once again the IB only let's me to connect the action to the custom cell class. How can I connect the action to another place?
Thank you for your answers in advance,
Dan
You should use the Object (the blue cube) from the object library. Drag and drop to the section (Documents outline) where your file's owner is present. Then in the identity inspector set it as the class (say Outsider) you want it to be. This will allow you to set Outsider object as the delegate/datasource and will also allow you to write IBAction's there.
The trick is to make that object(Class) visible in the xib by creating an object inside the xib (which will be done when the nib is loaded ).
And another thing, there is nothing wrong in setting the delegate or datasource programmatically, however through interface builder its much more convenient.
1) Do it programatically. Write your own delegate and assign it in code. Cleaner.
2) Connect the button's action in the ViewController class. That is clean, that's what view controllers are meant for. Turn on assistant editor and drag the "arrow" to the header file.
Question 1:
But the interface builder only allows me declaring as datasource (and
delegate) the file's owner, which is the view controller.
Question 2:
But once again the IB only let's me to connect the action to the
custom cell class. How can I connect the action to another place?
Both have same answer:
You can change the class by selectecing from interface builder.

Resources