ViewController's subview's IBOutlets nil in viewDidLoad from Storyboard - ios

I have a UIViewController that gets instantiated from a Storyboard.
In this view controller lays a MyView that is actually just a UIView from a Xib file.
In the view controller, I set my view as being a class of MyView and link it to an IBOutlet in my view controller class.
MyView contains a UILabel.
When in the view controller, in viewDidLoad I try to set myView.myLabel.text = "test" I get a fatal error: unexpectedly found nil while unwrapping an Optional value, myLabel is nil.
Can't figure out what's wrong.
Even in awakeFromNib() and in required init?(coder) my outlet is nil.
Any idea why and how to fix it?

Double check to make sure your link between storyboard and code is ok. I had this once when broke the connection and all I had to do was reconnect it.

See if you are using CustomViewClass in xib i would like to tell you first declare property as #IBOutlet var viewObject: CustomViewClass! in your Vc.swift & then give outlet opposite in storyboard & then inner subviews outlet declare in the CustomViewClass.swift
Now you are feel free to access in the your Vc.swift that innersubviews outlets of your CustomViewClass like
viewObject.myLabel.text = "test"
Don't forget to give Custom Class from identity inspector for file's owner Vc & your view CustomViewClass
Still if you have any doubt feel free to ask in comment.

Related

Loading a Swift based UIViewController from Objective-C

I am attempting to load a view controller nib whose File's Owner is a Swift based UIViewController class. The view controller is very simple at this point--just a single label (lblResult) whose contents get set at runtime. When loading the view controller, I get the following error:
this class is not key value coding-compliant for the key lblResult
I know that this is usually because the File's Owner is not set in Interface Builder, however, in my case it IS set. I have verified that the IBOUtlet for lblResult is properly set and that the view property is also properly set. The only real difference here is that I am attempting to load the swift based view controller within an app written in Objective-C.
In my view controller class I am setting the UILabel property as follows:
#IBOutlet weak var lblResult: UILabel!
and setting its value as follows:
lblResult.text = "Time \(hour):\(minute):\(second)"
In my Obj-C calling class, I am instantiating the view controller as follows:
viewController = [[UIViewController alloc] initWithNibName:#"SwfViewController" bundle:nil];
The nib loads into view when no outlets are set, but when there are IBOutlets, i get the error. Any clues as to why this is happening? Thanks!
If your nib is a View and the owner is a View Controller, make sure you connect your view controller's view property to the nib view. If your nib is a view controller, instead of setting the File Owner just set the Custom Class of the view controller to SwfViewController.
Is this view controller present in your storyboard? If yes, you need to load the view controller by calling.
viewController = self.storyboard?.instantiateViewControllerWithIdentifier("SwfViewController") as SwfViewController
Make sure you set the identifier of the view controller in the storyboard to SwfViewController.
The storyboard should look something like this.

Can't set view outlet in xib

I have a xib file that includes a view controller but I have the same problem as this link: problem
In this answer they can easily set outlet because view has a circle that is clickable.But in my case the view outlet is not even clickable.So I can't set the outlet.What to do now?
The correct way to use initWithNibName:: is to have a "View" IB document where you have the desired VC view outlet as a root element. You need to set the "File's Owner" "Class" to your UIViewController subclass and connect it's view outlet:
Also, don't present modal VC from self at viewDidLoad: at the time of this method execution the VC is often not yet presented itself, viewDidAppear: is more fitting for such tests.
I had this same problem, but neither this nor any other solutions seemed to work - what I wound up doing was setting the custom class to UIViewController, linking the view as described in the question link, and then changing the class back to the actual custom class name I had intended. The link stayed and everything worked from that point on.

Swift: Load .xib from storyboard, views not loaded

I'm trying to load a ViewController (a custom UIViewController subclass) from an .xib in Swift via the storyboard (I created a ViewController instance in the storyboard and deleted its view).
The ViewController itself loads, but nothing in the .xib loads with it.
In other words, my .xib looks like this:
ShapeView is a custom UIView subclass.
But if I look at the view which is actually loaded:
override func awakeFromNib() {
log.info("view \(self.view)")
}
self.view is just a generic UIView -- it's not my ShapeView.
If I try creating an #IBOutlet and connecting my ShapeView to it, the same thing happens -- the property bound to the outlet is nil:
If I use Obj-C for my ViewController class keeping everything else the same (the .xib and storyboard), it works as expected.
How do I set up and/or load the .xib in Swift via the storyboard to get the correct behavior?

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.

UITextField from NIB is not instantiated

I have to be missing something very obvious here...
I made a simple UIViewController with a corresponding nib. In the nib, I added a label and a textfield. I connected them to corresponding UILabel and UITextField objects on the view controller. In viewDidLoad, the UILabel object is instantiated, but the UITextField is nil (0x0). Is there some special action required to instantiate the UITextField? I thought that all UI elements from a nib were automatically instantiated.
jorj
When you define an IBOutlet as an instance variable, make sure the corresponding #property has the same name or you will post a dumb question on stackoverflow and feel very stupid about it!
jorj

Resources