Cannot control-drag in xcode between cocoa touch uiview class and subview in storyboard - ios

I'm new to xcode and iOS.
I'm following apple tutorial, but I can't figure out a thing.
In the tutorial, when there is the need of a connection between the storyboard and the ViewController.swift, it's done a connection using the control-drag from the storyboard to the viewController.swift, and it works like a charm.
If I add a subview to storyboard, apple does the connection and the declaration by code in a custom cocoa touch class that implements UiView Protocol, not using the control-drag.
Now, given the fact that is clear to me the code writted by apple, I'm just wondering why the control-drag doesn't work.
I've tried to do the same thing, adding a button via ui and then tried to control-drag to cocoa touch class, and I cannot do this.
But if I do the same thing, but trying to connect it to the ViewController.swift it works perfectly.
Now, I'm just wondering why this happens.
Can't I use control-drag with a custom UiView?
Thanks for answering.

You can only control-drag from outlet to view on xib, after creating outlet in code you will see a dot at the left side, you can drag from there to your view and outlet will work

In the storyboard, you have to set the class name to the view controller.

If you want to create a custom view that will have subview of it's own, you can do so using a Xib file (pretty much like a storyboard, but for only one view instead of for a whole app workflow) and then you'll be able to make all the connection you wish. Storyboard is not meant to design your customView. Connection are mainly used to provide a easy/cost-effective reach from the control grasp to it's main view's subviews (IBOutlets) or to '0 line' a target action pattern. One exception is with the custom tableView / collectionView cell though..

For connectioning between storyboard & class
you must in the
storyboard -> customClass -> chose your class

Related

I have One storyboard with different/multiple UIViews and I want a ViewController for each UIView. Swift

I have one storyboard with different views, I do not want all the codes in ViewController.swift I would rather have a Swift for each UIView.
Is it possible? and how do I do?
Use UIContainerView instead of just UIView. When you drag a UIContainerView into your storyboard it'll automatically create a View. Just make a new Cocoa Touch Class file in your project. Make sure the file is put under UIViewController
Then click on the new View that was automatically generated, go to Properties, then select your new Swift file. This connects the View to the file.

Loading a custom view in a storyboard from a separated nib file

I've customized an UIView in its own .xib file in order to being able to reuse it in several scenes of my storyboard. It is only a view with no view controller behind. I'm trying to load it in the storyboard and to see it in Interface Builder in the scenes I want to place it, with no success. I've been looking for a way to do that and I didn't found a post with an answer that worked for me.
I'm using Xcode 6.3.2 and my app is targeting iOS 7 and above. Could somebody help me?
Thanks in advance
What I do is make a subclass of UIView with its xib (CustomView.h, CustomView.m and CustomView.xib). And add a UIView to your controller on storyboard. Then assign its class to your custom view class.
This works without loading custom xib via code. But you cannot see the subviews of your custom view on storyboard.

Cannot create outlet connections to subviews in Interface Builder (Xcode 5)

I know this appears to be a duplicate of some other questions, but the answers are not working for me.
I have created a single view app.
In the storyboard I added a subview to my main view.
I have a label on my main view and another label on my subview.
I have created a class of type UIView and added it as the custom class for the subview.
I can ctrl-drag my label on my main view to the main view controller class. But when I try to ctrl-drag my label on my subview to my custom class, I cannot get the connection to occur.
I have even typed the property information and tried to make the connection manually to no avail.
Things have changed a bit in the latest version of Xcode's Interface Builder. Can somebody tell me what I am missing? There is literally no code here. I am just testing trying to connect outlets to a subview with a custom class.
The first image shows that I have set up the custom class and added a property but I cannot make the connection.
The second image shows the main view label is connected in the main view's controller.
The third image shows that there are no outlet connections for the subview's label.
You can manually write the IBOutlet property declaration in the #interface of the custom view subclass, and assuming you've defined the base class of your subview in IB, then you can drag from the outlet circle in the code back to the control in the scene.
Or, as you point out, Warren Burton suggested both this technique and another in his answer to this other question, Can't Wire to Subview in IB.
The issue has to do with the File Owner of the View Controller. It is probably set up as being IOViewController, thus you can only make property connections in that .h file.
What you can do, is create another .nib file for the subview and put the subview in there. Then in that .nib file, make the file owner IOSubview. Property connections will work just fine there. Then just add the subview to your IOViewController programatically. Just remember to load the nib file from bundle first.
This is what I did (in Swift):
I Created a new ViewController (e.g. class MyViewController: UIViewController {})
In StoryBoard, I expanded the 'Scenes' (i.e. the tree view of all UI components) and selected 'MyViewController'
Using the 'identity inspector' I assigned the 'MyViewController' class (as oppose to the default UIViewController)
After that I was able to assign an action.
I suspect that for Obj-C it is similar process.
You don't create outlets in the subclass, you create the outlet on the view controller it is on. You need to #import the subclass into IDViewController.h and create an outlet there.
IDViewController.h
#import "IDSubclass.h"
...
#property (strong, nonatomic) IBOutlet IDSubclass *outletName;
Zoom your storyboard to 100%. If you zoom out, to say 50%, then the outlet connection won't work.

How to connect UIView outlets to a custom subview

I'm still new to xcode / iOS and have the following problem:
in order to display some mobile debug information, I have a UIview added/connected as outlet-property to one of my Viewcontroller. This view is a custom subclass of UIview. Now I addeddd some UIlabels as sub views to this view and want to drag the outlet connections from these labels to my customUIview.h file in order to have these labels accessible as properties of my custom UIview class (no need to access them directly from the view Controller).
Problem is that the interface builder (I'm using Storyboards/ xcode4.3) does not make the trick. I can connect the outlets to the ViewControllerClass.h but not to my sub view's .h file.
Can anyone point out where the problem is?
Just solved the problem. After typing the property outlet declarations manually in the customview.h file I could ctrl-drag-connect them from there to their corresponding UIlabel objects in the interfacebuilder. Works only in this direction!
Thanks anyway
Update your custom view class from "UIView" to your "Custom View" class in interface builder.
And now you can make connections just by ctrl+drag your buttons or textfield to your appropriate custom class.
Problem solved! Due to an interesting SO post from three years ago (about connecting to subviews of UIView), I discovered that one merely drags (not Ctrl_drag!) from the action or outlet circle (in the .h file) to the control and that's it. Works perfectly even when the controls are in a different view from the subclassed UIView. Works equally well with outlets as with actions though you always drag away from the circle.

Can't set custom class name

I can't seem to be able to set the identity of my view to GraphViewController in Xcode 4.2.
I dragged a regular UIView in storyboard on a UIScrollView.
I create a new file called GraphViewController extending UIViewController (.h and .m) but when I click on my view in storyboard and try to set the custom class name (its identity) to GraphViewController, it doesn't set it. It doesn't even show it in the dropdown list.
Am I missing something here a connection or something?
You can set any class inherited from UIView to a UIView instance.
But you have created a UIViewController subclass, not a UIView subclass.
You can assign your custom class to the controller, not to the view.
In case anyone ever makes the dumb mistake I just did... ensure you have created an appropriate storyboard for either iOS or OS X. Adding the wrong one to your project will certainly cause issues like this.
My solution was making sure I was clicking on the controller's top tool bar, and not the "screen" UI frame. If you select the frame or anything in it, your Identity Inspector will be looking at the View, or other elements in the storyboard, instead of the controller itself.

Resources