Subclass of UIViewController is released immediately - ios

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.)

Related

How would I use polymorphism to allow a method to accept multiple classes, but with IBOutlets?

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

programatiically creating views strong properties

Previously I've been creating my views with interface builder.
When creating views with storyboards or nibs I would connect my outlets. I understand that the outlets are creating a strong connection to the properties on the view.
If I am creating views programmatically should my properties be strong?
.h
#interface LoginViewController : UIViewController <UITextFieldDelegate>
#property (nonatomic, strong) UIView *loginView;
#property (nonatomic, strong) UITextField *usernameTextField;
#property (nonatomic, strong) UITextField *passwordTextField;
#property (nonatomic, strong) UIButton *signInButton;
#end
.m
#interface LoginViewController ()
#end
#implementation LoginViewController
- (void)initViewsAndLayout
{
_loginView = [[UIView alloc] init];
_loginView.frame = self.view.bounds;
[self.view addSubview:_loginView];
//...
}
#end
A few things:
Your code isn't using the properties you defined
Don't put the private properties in the .h file
Don't state that your class conforms to the UITextFieldDelegate protocol in the .h file
As for whether the properties should be strong or weak I prefer strong but since you will be adding each of these properties (views) to the view controller's view, it would be fine to make them weak since there will always be a reference to them as long as the view controller is alive.
Your .h should just be:
#interface LoginViewController : UIViewController
#end
Your .m should be (assuming you do want to use the properties):
#interface LoginViewController () <UITextFieldDelegate>
#property (nonatomic, strong) UIView *loginView;
#property (nonatomic, strong) UITextField *usernameTextField;
#property (nonatomic, strong) UITextField *passwordTextField;
#property (nonatomic, strong) UIButton *signInButton;
#end
#implementation LoginViewController
- (void)initViewsAndLayout
{
self.loginView = [[UIView alloc] init];
self.loginView.frame = self.view.bounds;
[self.view addSubview:self.loginView];
//...
}
#end
When you use the strong attribute, you are basically writing retain, as in, you want Objective-C to allocate memory for the property and hold it until it is released.
That is all strong means.
This is only necessary if you are using ARC (though you can still use retain in ARC). If not, use retain.
"If I am creating views programmatically should my properties be strong?"
If they are objects and not primitives, then yes.
I recommend you to make properties with strong attribute if you want to create it manually not to use xib or storyboard.
As your code in initViewsAndLayout, if you always create a subview and add it to a view of a viewcontroller simultaneously in same method scope, there is no problem even if you use weak or assign attribute. addSubview will increase a reference count of the subview. But, I don't think it's such a good habit because all of us could make a mistake.
Therefore, you had better make a property of UIView with strong attribute.
You asked:
If I am creating views programmatically should my properties be strong?
Two part answer:
The view controller should definite maintain a strong reference to its top level view.
When not using NIBs or storyboards at all and doing everything programmatically, the root view is instantiated in loadView and generally stored in a retain/strong property. UIViewController already has a view property that bears the retain memory semantics, thats generally used. (For more information about programmatically created views, see the Creating a View Programmatically section in the Resource Management in View Controllers chapter of the View Controller Programming Guide for iOS)
(If you're not creating the root view in loadView and are instead instantiating the root view with a NIB or storyboard and are only programmatically creating the subviews, don't worry about the above discussion, as the NIB/storyboard takes care of all of that for you.)
For the subviews, when you call addSubview, the view is retained by its parent view. It's unnecessary for the view controller to also maintain strong reference to it as well. You can, but it is not necessary.
In my mind, the view owns its subviews, not the view controller. If one of these subviews is removed from its parent view, I don't think the view controller should be retaining it and it doesn't seem like good design that I have to remember to nil the property in the view controller, too. Worse, if I remove a container view that has nested subviews, I don't want to have to manually keep track of setting all of those individual properties in the view controller that I have to nil, as well.
Don't get me wrong: You can use strong with the subviews if you want/need. But I think it's incorrect to imply that it's ill-advised to use weak.
As a simple rule of thumb,
Any view is always owned (maintained by strong reference) by its
superview, when they are added to the superview.
A top-level view is always owned (maintained by strong reference) by its VC.
Now, a view can be added to its superview in two different ways,
When a view is created from code and added to some superview using addSubview: method. (This is what your question suggest).
When the view hierarchy is loaded from a nib file. (Here also implicit addSubview: calls are made by UIKit to create the hierarchy) and thus subviews are retained by strong reference their respective superviews.
So in either cases views are implicitly retained by their superviews and thus never get disposed until/unless the top-level view gets disposed by itself. So declaring strong properties for subviews imposes another ownership on the subviews:
An implicit ownership by its superview, and
An explicitly ownership by the property.
So in general, declaring strong property for subviews are not required, however it has been created (from nib / by code).
However, under some special situation declaring strong properties for views might be required. For example, when a view should be removed and re-added to its superview, the view should be declared as strong. When such a view is removed from its superview, the superview releases its ownership as well. So if we need to maintain a strong reference of that view if we need to reassign the same view again to some superview. In that case, a strong property reference to the view becomes handy and it disallows the view to be released.
PS:
When creating views with storyboards or nibs I would connect my
outlets. I understand that the outlets are creating a strong
connection to the properties on the view.
I found that there's merely a misconception, in your question. When creating view from storyboard/nib, outlets does not create any so-called strong connection with the properties. IBOutlets only describe how a property loads a view. In case of an outlet property, the view pointed by the property is unarchived and loaded if it has not already been loaded. However, its retention still depends on the strong/weak property accessor.

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.

Dealloc of UIView subclass prevents running class methods

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.

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