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

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.

Related

Creating IBoutlet from UIView in custom UIView class swift [duplicate]

This question already has answers here:
Cannot create outlet connections to subviews in Interface Builder (Xcode 5)
(5 answers)
Closed 6 years ago.
I have a UIView with a UIlabel and a UIImage inside it placed on my view controller.
I created a custom class for the UIView and tried to create an IBoutlet for the UIImage and UILabel in the custom class but i will not let me drop it.
I have already clicked on the UIView and set its class to the custom class, so I am not sure why it is not letting me create the outlet.
Below is a screenshot of the custom class, uiview (With image and label). You may have to click on it to see it.
Thanks for your help
In your situation, you should connect the outlets to a view controller instead of a view.
Instead of creating a UIView subclass, you should create a UIViewController subclass:
class FrontViewController: UIViewController {
}
Next, select your view controller in the storyboard. In the Identity Inspector (select the tab with an ID card on the panel on the left), type "FrontViewController" (the name of your class) into the "Class" field.
Now you should be able to drag outlets to the class!
As you are crating ViewController there you can connect only view controllers IBOutlets. What you are doing with setting this views class is only marking that this view should behave as this class (you are not creating it there, so you can't set it's IBOutlets). As well this to items won't have anything with tat view, as it's not it creation, that will just be two separate views and YOUR view will be as parent. The only thing you need to understand is that you can create IBOutlet only if it's views creation part, as it's only controllers creation part it's won't give you possibility to create such for his inner view.

Ios single xib with multiple views

I've noticed that i can put in a single xib multiples UIView; the main view is associated with the file's owner, but how to reference other views in the xib?
My need: i've a xib view splitted with a fixed top part and a bottom part made by a tabbar with three tab buttons: by clicking each button i need to load a subview, so my idea is to put other sub-views in the same xib on other views and load them on demand. How to accomplish this?
Thanks
You can just create IBOutlets in your header files and associate them with the additional views, just like any other Interface Builder component (or even just ctrl-click and drag your views to your code, if you're working in XCode's automatic assistant mode).
For example, let's say you have a view controller called FooViewController, and a matching FooViewController.xib interface file:
#interface FooController : UIViewController
#property (nonatomic, retain) IBOutlet UIView *additionalView;
#end
...and then you can just connect your additional view up to its corresponding outlet (which will appear in IB under the file owner). It's really no different to hooking up a UILabel or UIButton.
One thing to note though - you say "my idea is to put other sub-views in the same xib on other views and load them on demand". All the views inside your XIB file will actually get created at the same time, so it's not really loading on-demand. I doubt, unless you're doing something crazy, that this will be an issue for you in practice.

Why won't my IBOutlets show up in my list of Outlets in Interface Builder?

I basically have code that reflects the answer from Connect a UILabel in Interface Builder and XCode?.
In one view controller I have several UILabels whose text is set by some other methods I have written in viewDidLoad. This has worked fine, but today I tried adding a couple more labels in the same fashion and I can't get their references to show up in the Outlets list in Interface builder. How does identical code not work in the same view controller?
1) Make sure you have written the IBOutlet property correctly,
2) File's Owner Custom Class of Your .xib should be your ViewController class
3) If you right click on the File's Owner which is listed under Placeholders, The IBOutlet myLabel should be there,
4) And make sure you have connected the IBOutlet to your UILabel.
5) Set the text of the Label from your viewDidLoad method,

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