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
Related
I have a beginner's question.
I have a view with a button, which switches to another view by a segue.
In that case, must this button be declared in ViewController.h, like this:
#property (weak, nonatomic) IBOutlet UIButton *calculate;
or is it optionnal?
I remark that the segue transition works without button declaration.
Thanks in advance !
Short answer: No, you do not need to connect the button to an IBOutlet.
Not-so-short-answer: You only need to create an IBOutlet for objects that you want to reference from code. Such as if you wanted to change the Title on the button for some condition. This would have no effect on your Segue connection.
The declaration is optional if you don't want to access the instance of the button in your code (of course, you can still access it with viewWithTag or by getting the subviews of the main view, but it's much cleaner with a declaration).
You don‘t need an outlet here.
The IBOutlet doesn‘t declare the button. The button is created by the XIB or Storyboard file. An Outlet is simply a name under which the XIB/Storyboard-loading mechanism will store the address of the button so you can modify its title or whatever.
If you create a button yourself, you have its address, but if the button is created by the XIB, you need some other way to get individual objects‘ addresses out. That‘s what an IBOutlet is for.
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.
I have a specific method that accepts a UIView object, and I want to pass objects to it that can be of one of two classes. So say it accepts a UIView that represents an animal, I want to be able to pass a DogView and a CatView or other animal-type classes potentially.
Within that method I want to set the nameLabel view, which all animals have. How do I set it up so I'd be able to do this?
My first reaction was to have a super class (such as AnimalView) that has the nameLabel variable on it, and then subclass it for each new animal. However, if I want the nameLabel to be an outlet, it doesn't seem settable as I couldn't have the variable in every subclass to wire the view up to in IB.
I then tried a Protocol, but that's not polymorphic and I wouldn't be able to access the nameLabel property through a generic superclass, could I? Unlike Objective-C I couldn't ask for a UIView <ProtocolName> and it would then allow me to ask for it.
How should I be doing this? I just want to be able to pass different kind of objects and have it be compatible with Interface Builder. Should I be approaching it completely differently?
You can connect outlet of label to different viewControllers with your SuperClass from story board if your different viewControlelrs in storyboard reperset by Subclasses(derived from SuperClass) names in storyboard.
1)Just define
class SuperClass{
#IBOutlet weak var label: UILabel! = nil
}
SubClass1 repersent view controller1 in storyboard derived from SuperClass
SubClass2 repersent another view controller2 in storyboard derived from SuperClass
2)Than Go to Assistant Editor and open SuperClass one side and other side view controller1 and connect outlet from SuperClass to label in storyBoard in view controller1.Drag from SuperClass label to storyBoard in view controller1
3)Now again open SuperClass one side and other side view controller2 and connect outlet from SuperClass to label in storyBoard in view controller2.Drag from SuperClass label to storyBoard in view controller2
If you click on SuperClass outlet than you will see two labels conneted to different viewControllers
Declare the IBOutlet in a superclass, AnimalView. Then in Interface Builder, once you have set the custom UIView's class in the Identity inspector to be DogView, go to the Connections Inspector and your nameLabel will be there.
#interface Parent : UIView
#property (nonatomic,weak) IBOutlet UILabel *nameLabel;
#end
#interface Child : Parent
#end
I've created a UIView subclass with a UIButton inside with target on tap inside.
The selector is implemented inside the UIView subclass and linked by Interface Builder.
But when I run addSubview in parent view controller tapping button will cause
performSelector:withObject:withObject:]: message sent to deallocated
instance
(I use ARC)
I added some console output in dealloc method and I see that right after viewDidLoad ends with addSubview of the UIView subclass, the sublass is deallocated.
How to prevent that sort of situation?
I tried #property(nonatomic, retain), adding instance to some global array...
But no luck.
I understand that ARC releses the object as there is no strong reference left but I couldn't force to prevent that situation.
Any help would be appreciated.
Creating a strong reference of your subview class in your View Controller class may help you solve the issue.
#property (strong , nonatomic) UIView *subclassName;
Are you storing reference of button (UIButton) you are trying to add as subview?
The problem was in linking NIB to Class.
I linked File owner to class instead of linking the UIView object.
Normally when there is working on nib with UIViewController, there is a need to link File Owner to UIViewController class.
But when working on UIView subclass the UIView object must be linked.
Subclassing UIViewController I have UIView within NIB file then add "Object" with Custom Class named CustomViewController, this class have view property connected to the UIView from NIB file and other IBOutlet UIImageView connected within Interface Builder.
My question if why this class is loaded from NIB (initWithNibName is called) and is released immediately.
Everything loaded in a nib is autoreleased. If you don't explicitly retain it, or connect it to an IBOutlet property with retain stated in the property definition, it will go away. (And usually make you unhappy.)