Xcode add collection view cell to collection view on interface builder - xcode

I am relatively new to using XCode interface builder.
When trying to drag a collection view cell onto an empty collection view, the interface builder refuses to do so.
Please have a look at attached screenshot:
Am I doing anything wrong?
Note that I can do the same on a storyboard but not on a normal xib file.

You can only add a UICollectionViewCell inside a UICollectionView when working on a Storyboard file. For sure, you must be using a single nib file and that's why it isn't working.
If you wanna use a custom UICollectionViewCell, then you must create a subclass of UICollectionViewCell. When subclassing UICollectionViewCell don't forget to check the box that says "Also create XIB file" beneath the expandable menu of "Subclass of". Now, try to drag a UICollecionViewCell onto the canvas and custom it the way you want.
Another posibility would be using a Storyboard file. Try setting your UICollectionView inside a scene in the storyboard and then drag a UICollectionViewCell inside it. When dragged, create a UICollectionViewCell subclass and assign it to the UICollectionViewCell you dragged previously inside your CollectionView in the Identity inspector.
In either case, don't forget to set the reuse identifier of your UICollectionViewCell in the Attributes inspector :)
Here is a code sample from Apple of how to create a UICollectionView with a custom UICollectionViewCell using a storyboard.
https://developer.apple.com/library/ios/samplecode/CollectionView-Simple/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012860
Hope this helps :-)

Related

Creating an outlet for a NSProgressIndicator inside a NSToolbar

I have this OSX storyboard-based application that starts with a NSSplitViewController like this:
This splitViewController has two viewControllers: master and detail.
Inside the window I have a NSToolbar. I dragged a NSProgressIndicator to that toolbar and Xcode embedded it inside a NSToolbarItem.
Now I need to create an outlet (not an action as explained on other stackoverflow questions) from the NSProgressIndicator to some class. First question is which one?
Xcode will not let I create an outlet. I have tried these options:
dragged from the ToolbarItem to masterController class file, detailController class file and to NSSplitViewController class.
dragged from the ToolbarItem to the delegate class.
dragged from the NSProgressIndicator to masterController class file, detailController class file and to NSSplitViewController class.
dragged from the NSProgressIndicator to the delegate class.
dragged from both the NSToolbarItem and from the NSProgressIndicator to the Window Controller First Responder.
In all cases dragging does not make a window appear to allow me to create the outlet.
For heavens sake, how do I create an outlet like that? To which class I drag it and how do I do that?
I'll assume your setup is more like this image:
Your Window scene is backed, by default, by an NSWindowController, to which you cannot add new outlets. You need to create a subclass of that, associate it with your Window and then you should be able to create outlets in that.
File > New File > Cocoa Class
Specify a name like "SpaceDogsWindowController", as a subclass of NSWindowController.
Then use select the window controller icon (blue circle) and select the Identity Inspector in Xcode. (CMD+ALT+3). Specify the name of your new class in the "Class" field.
Then try to hookup an outlet:
1) Show the Assistant Editor
2) Use the Jump Bar to ensure your custom class is visible (It's at the top of the assistant editor pane, it should say Automatic and you can tap that to then select your new class; If it says 'Manual', change it to Automatic).
3) If your are control-dragging and it's still not offering to make a connection, try dragging from the document outline (also shown in the screen shot).
You could then edit that progress indicator from other view controllers, which are descendants of that window's view hierarchy, using code like this:
if let windowController = self.view.window?.windowController() as? CustomWindowController {
windowController.progressIndicator.doubleValue = 0.4
}
or, in Objective-C, something like this:
CustomWindowController *myWindowControllerSubclass = self.view.window.windowController;
windowController.progressIndicator.doubleValue = 0.4;
Hope that helps.

Views within custom UICollectionViewCell not receiving touch events

I have a UICollectionView which supports horizontal and vertical scrolling. Each collection view cell is a subclass of UICollectionViewCell and they are loaded from a NIB that contains UIButton's, UITableView's and even another UICollectionView. They are all inside a TPKeyboardAvoidingScrollView.
Scrolling works perfectly both horizontally and vertically, but it is not receiving touch events on the elements on the individual UICollectionViewCell. When I touch any of the elements, say a button that's part of the UICollectionViewCell, then all that I get is the didSelectItemAtIndexPath event on the UICollectionViewDelegate - I am unable to get any touch event passed through to one of the subviews of the collection view cell the tap occurs on.
After going through some other stackoverflow articles I eventually found what caused my problem - the XIB file I used to initialise the UICollectionViewCell had a normal UIView element at the root; even though it was marked as a subclass of a UICollectionViewCell and behaved fine in any other way, that prevented the events to flow through.
So - if your custom UICollectionViewCell XIB file looks something like this at the the root (see the icon next to the Data Entry Matrix View, which is the name for my UICollectionViewCell subclass), then you will have the same problem:
My solution was to add a proper UICollectionViewCell class to the XIB, then move all the subviews under the existing Data Entry Matrix View into it, delete the old root, then reconnecting the Outlets. The end result looks like this:
You can see the different icon used - that's what you want. Once I'd change my XIB like this the events were passed through.
In the raw XIB file (view as Source), the non-working version introduced the root element as
<view ...>
whereas the working version changed that root element to
<collectionViewCell ...>
Hope this saves someone else a couple of hours...
a have the same problem for UIButton in my custom UICollectionViewCell
Adding that function to my UICollectionViewCell solved problem for me:
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
self.contentView.frame = bounds;
}
If you have a view/scrollView at the root and it is attached to backgroundView or selectedBackGroundView remove it and it will work fine.

Can't ctrl+drag into ViewController.h from second Viewcontroller

I am building an iOS app for fun and here is where I run into trouble. I can insert an outlet and action in the ViewController.h files directly from my first View Controller through the ctrl+drag method; however, when I try ctrl+drag on the second ViewController it will not allow me.
Ctrl+drag on first ViewController
Ctrl+drag on second ViewController
You have the wrong document open in the assistant editor. It should be ViewController.h, but you are displaying UIViewController.h.
Check you have correctly set your second view controller to your custom class ViewController using the Identity Inspector (third of the right hand utility panels) then make sure it's header file is the document you are displaying on the right.
update
From your comments, you are having difficulty setting the second view controller to a custom class.
Here is how you select it in the storyboard. Note that you are selecting the View Controller, not it's topmost View
Copy and paste the class in the field "Class." You then need to press ENTER to take effect. Example

Replace subview of NSSplitview with custom view

I still have a lot to learn with cocoa so I may have missed something obvious here. I have a custom view I would like to display in an nssplitview which replaces the current subview there.
I have a MessageView.xib file, and a MessageView .h/.m which subclasses NSView. I created a custom view instance for my main window (the one which contains the nssplitview) through Xcode 4's built in gui builder. I created an outlet to this instance of MessageView in my window's controller.
In my controller for the window when I want to swap out the subview for the splitview it runs this
[splitView replaceSubview:[[splitView subviews] objectAtIndex:1] with:viewMessage];
viewMessage is the outlet to the MessageView.
When this code is run the display of that subview changes to be blank. I'm not sure if there is something wrong with my custom view or there is some size issue. Is there something I need to do to fit the view into the split screen view or is my custom view just not displaying correctly? I have had a difficult time finding a tutorial on creating custom subviews with Xcode 4 so I'm not sure if something could be wrong with that. The custom view just has a label and a textfield in it.
Generally, you shouldn't need to replace NSSplitView's subviews with your own. Rather, you add your own custom view(s) as child views of the default subviews on each side of the divider. You can do this in code with addSubview:, but it's probably easier to just use Interface Builder in Xcode. Drag a "Custom View" into the splitview, then in the Identity Inspector, under Custom class, change the class to the name of your custom NSView subclass:
I think (off the top of my head, not tested), if you really do need to replace the default NSSplitView subviews with your own class, you can probably do it in Interface Builder using this same method, but selecting the default subview itself and changing its class in the inspector. This doesn't work for all AppKit classes, but it may work for NSSplitView.

Create a custom view using a nib and use it in a window

I have been struggling quite a bit with this problem and I can't seem to figure it out by myself.
This is the situation:
I want to have a custom Nib-based view, with its own ViewController. I then want to add this view to a window. Therefore the window should contain my custom view.
So I go about and create a very simple example like this:
In Xcode 4 I create a new blank document-based Cocoa application
I create a new Cocoa Class which should extend from NSViewController (which causes the nib to be created along with the .h and .m file. The name of the created .h, .m und .xib file is FaceViewController for testing purposes it should only display text for now.
I open the NSViewController.xib in InterfaceBuilder and add a multi-line text component and place it in the custom view, which is already in the xib file. I make the text span the whole view.
In MyDocument.xib I also add a Custom View place holder, which should be replaced with my custom FaceView.
From this starting point on, everything I tried to include the created View + ViewController on my MyDocument.xib failed and the area where my text should be shown remains empty (just like the background of the underlying window.
So my question really is what I need to do, so that my FaceView gets loaded into the Custom View which I placed on MyDocument.xib. Here are some things which are unclear to me:
The custom View extends from NSView so I wanted to change this to my FaceView but the view does not exist as a class but is defined in InterfaceBuilder in the xib, do I need to set this to anything other than NSView?
Do I have to alloc, init the FaceViewController in code or is it enough to drag it into either of my two .xibs?
Can I use InterfaceBuilder to place my FaceView or do I have to do this programmatically?
I really thought that creating a custom view like this would be a piece of cake but it turned out quite the opposite so far, so any help would be greatly appreciated.
You can create your FaceViewController either by adding one to the MyDocument.xib or by creating it with alloc, init.
To place your FaceView you'll have to do it programmatically, you can use
[customView addSubview:[FaceViewController view]];
of if you want to replace the view
[customView replaceSubview:oldView with:[FaceViewController view]];

Resources