I'm trying to get a handle on what's happening in my app...
I have a UICollectionViewCell subclass which has a UIImageView as a property.
In that subclass's init method, I programatically add the imageView property as a subView of the classes contentView:
[self.contentView addSubview:self.imageView];
At the point this happens, the 'self.imageView' property is null as the imageView has not been created and assigned to the property yet.
Later in my cellForItemAtIndexPath method, I assign the imageView property of the cell equal to an imageView object.
At this point I expect that the content view would now have a valid imageView subclass, but that's not what i'm seeing.
So my question is: Why doesn't it work?
Should I be able to add my 'null' property imageView as a subView of my self.contentView and then later assign the imageView property equal to an imageView object, and then see that object as one of the contentViews subviews?
I hope this question makes sense, it was a pain to describe!
Adding a subview which is nil sounds wrong to me
I think you are using a wrong approach. You should instantiate and add your imageView in your cell´s init method and then, in the cellForItemAtIndexPath of your controller assign the image like this
cell.imageView.image = the image you want...
No, you shouldn't (and you see it doesn't work). A property is a reference to an instance of something. If that instance doesn't exist yet then you can't work with it. Trying to isn't setting up some rule for future use, it's just quietly doing nothing.
You can add a method like configureImageView: and call that instead. The implementation would set the property and add the image view as a subview.
Related
I'm targeting IOS 8+.
I have a form that is used in more than one place. So I decided to create a custom view where I define the various "form" text fields.
I have built my XIB, and the UIView subclass contains the outlets for each textField.
The view is composed of a background image and a scroll with the form fields over it.
Now, my first obstacle was: I need to have this custom view in a container that may or may not have a navigation bar. This made me create a constraint outlet so I could update its value to push down the scroller view. This way I'd have the whole image in the frame, the top being behind the navbar and the scroller bellow the nav bar).
Here's a manual drawing to help understanding the problem.
It's very possible that I'm making a lot of mess and confusion on my way to solve this. :)
The problem is:
After awakeFromNib runs I have no access to the constraint property. I then noticed the same thing happens for the TextFields outlets.
So, how can I access the custom view's properties when I instantiate them programatically?
Something like:
Controller:
let customView = SignupView(frame: f)
view.addSubview(customView)
customView.pushScrollerDownBy(50.0)
Custom view:
func pushScrollerDownBy(yOffset: CGFloat) {
//topScrollerConstraint is the outlet for the textField.
topScrollerConstraint.constant = yOffset //right now topScrollerConstraint is nil.
}
You should check if you have connected your topScrollerConstraint to the file's owner since it will not get instantiated and therefore, error. Here is a recent SO question regarding difference between these two:
What is File’s owner in XIB in this case?
WORKGROUND
Create a UIView with UILabel and UIButton. Here i have set layer properties of UIButton below:
[self.btnDropdown.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[self.btnDropdown.layer setBorderWidth:0.8f];
[self.btnDropdown.layer setCornerRadius:4.0f];
Now i am saving this custom view into file using NSCoding and load the same. This control is displayed in UITableViewCell.
ISSUE:
All the details of custom view like UILable title, size, background color etc. Same for the UIButton are retrieved from file. But CALayer related details which i have set for UIButton are not retrieved.
Before saving to file:
After getting from file:
So I am not clear about that CALayer properties are manage with UIButton object.
Is these details (CALAyer values) are visible only at run time? Or i am missing something?
Please add a comment if question is unclear to you.
EDIT:
Previously i have not provide more information to reduce the complexity of the question. Below is complete hierarchy of my implementation.
NSObject --> Custom View --> UIButton --> CALayer
1) First NSObject class is a bean class. Which contains some variables (i called it Property). This properties are meta details of Custom view. There is one variable "parentView" is use to store Custom view.
2) Custom view: This is UIView class which contains UILabel and UIButton
Now bean class object (Property) is stored in file and retried. So that i can use variable parentView to load Custom view.
And as per question about the UIButton is embedded in that Custom view.
Figure it out:
Here button shown in images custom views. I used to save these control in local database with BLOB.
Thing is that these views object/instance are stored in database with some properties.
Now when i retrieve them and add them to any view as a subview their -init method is not getting called. (Any how i figure that out) and called -init method for these views.
I am sub-classing a UIScrollView element in response to a previous question suggestion.
In my subclass I have a few methods that are triggered by touch and keyboard events. Those events need to interact in various ways with variables that are in my superview.
I've tried accessing them via _myVar.text = #"smth" but that doesnt compile.
I know I could play back and forth with NSNotifications but is there a way to access the variables in my superview / class from my UISCrollView subclass?
I've tried self.myVar = #"" but it says that it is not recgonised. I am subclassing an UIScrollView element but then my view that hold all my variables is a UIViewController. just the UISCroll element was subclassed
Everything that is a #property in your superclass is accessible in your subclass, but not in the other way
It sounds like you're a little confused here. Are you sure you're talking about the superview? Or are you talking about the superclass from which you derived your subclass? If superview, and if you know the type of the superview, get a pointer to it, cast it to the appropriate class type, then call methods on it or access its visible properties.
UIButton* superButton = (UIButton*)[self superview];
superButton.visible = NO; // Assign to superview property
If you're talking about the superclass (in your case UIScrollView), then yes of course you can use any of its visible methods and properties.
self.bounces = YES; // Assign to superclass property
In case it's not clear what the difference is, you should really do some more reading about the basics of Objective-C and object oriented programming.
Superview: the view relative to which this view is laid out. This view is a subview of the superview. UIView and all derived classes have this hierarchical relationship.
Superclass: the class whose functionality you are extending to define your subclass. Your subclass inherits all visible properties and methods.
So much confusion, where to start? I really don't know. For what it's worth,
_myVar.text = "#"smth"
will obviously not compile, note the stray ".
_myVar.text = "#smth"
has a better chance of compiling.
I have a custom UICollectionViewCell with an imageView, a textField and a label. I have a skeleton made in Interface Builder. I need to do some more work on the imageView (setting the image and giving it rounded corners) during the initialization process, but I can't do it during initWithCoder: because the outlets aren't set yet. I would like to do that work within the cell class so I can keep those components as private. Is there anyway I can do the work on those components during the initialization process, or do I need to make them public so that my collection view data source can do that work during cellForItemAtIndexPath:?
Do it awakeFromNib instead of initWithCoder. The outlets wil be set by then.
I have a UIView subclass. The view has a float property. I initialize this subclass from another view (it's superView) like so:
myView = [[CustomView alloc]initWithFrame:aFrame];
The problem is, I am confused about how to assign values to myView's float property. When I try to assign a value just after initializing (from the superview's initWithFrame), nothing happens. Even when I try to to assign from another method or from the viewController it does not work.
Thanks for reading!
Most likely your class is trying to use it in a method that is called before you set it. Make a custom initializer such as -(id)initWithFrame:(CGRect)frame andFloat:(float)f. This will allow you to set the float when the object is created, so it will have that value upon instantiation. If this is not the issue, we will need to see more code!