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.
Related
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.
I have a flow issue in my iOS app from my subclassed UIView to its parent UIViewController.
Basically, I have a nib called preferences. It contains two sliders, two labels, and another UIView that will display a shape dictated by the two scroll bars (Stroke and opacity). I successfully painted the subclassed ui to the screen by setting the custom class of the UIView to a separate UIView we'll call subView. I have setters/getters for the scroll bars and they print out their values. How do i let the uiview class (pointed at the ui nib object) to update and redraw since it isn't referenced in the parent preferences class? I tried syntax like this:
[code]
IBOutlet SubClassUIView *subclassUI
[/code]
to no effect. It seems best to point a custom class at the UIView.
Any suggestions and advice would be much appreciated.
TL;DR can't modify subclassed uiview from "parent" uiviewcontroller
Sorry folks, i figured it out.
In addition to passing in a custom class that shares the same type as the IBOutlet (such as a UIView),
there MUST be a link referencing every object in in the .xib to its parent. In this case, the parent UIViewController needed a reference from the custom class ui to a IBOutlet UI. From there, some simple casting from generic and boring UIView to the custom class's unique methods makes for a complete flow.
In my project i have an UIView instance and attached a gesturerecognizer to it. In case that gesture is recognized i want to call its parent viewcontroller to reorganize the scene.
Here's how it's implemented
The UIViewController is called HomeViewController.
It has one subview which is a UIScrollview.
The UIScrollView contains several UIView instances.
To all of these UIView instances i attached a gesture recognizer. When it fires i want to disappear and call the HomeViewController's reOrganizeUI method.
My problem is that i can't reach the HomeViewController from the UIViews.
Is there a way to do this?
Sincerely, Zoli
Surely you can. The simplest way to do this is to add a property to all your UIView subclasses and assign your HomeViewController to that property when creating the views in the view controller's initialization method. Then you'll be able to access the controller from the views directly.
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.)
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