I have created a UIImageView subclass. I have both the .h and .m files. I have a property in my UIImageView subclass. I dragged a UIImageView in a UIViewController. Now, I want to give a default value to the property. For example, i have an enum, and according to that enum value, i want to set the image of my imageView. How to give this value, when i am not calling the init method, i am loading it directly from the xib. I am stuck on this. Spent a lot of time, but not able to work it out.
Ok i worked it out. I overrided the method:
initWithCoder:
and instantiated the values in that method!
Related
where do you programmatically set text for UILabels in a UIView?
I tried in the init method and this doesn't work. It doesn't set the text.
I've put the code in the parent controller that uses the UIView within it, but to me it would make more sense if I put it somewhere in the UIView code.
Background - doing programmatically has I'm doing localisation on the text
Your initialization code should be in initWithFrame:, UIView's designated initializer.
Alternatively you can implement awakeFromNib which is called on an object (such as a view) once it has been unarchived from a nib file.
In -init methods outlets from XIB do not exist yet.
I set the text for that kind of controls in -viewDidLoad routine
I have this UIViewController which contains an UIScrollView.
In the viewDidLoad method of the first controller, I create some UIViewControllers(defined through a NIB with an UILabel and an UIImageView inside,linking to IBOutlets done correctly), then I add them to the UIScrollView.
Problem is, I can see my views and scroll through them using the scroll view, but I seem to be unable to modify the text of the label and the content of the imageView using the methods [[UIViewController UILabel] setText] or [[UIViewController UIImageView] setImage]
I know it sound like a stupid question, but I can't get past it.
Any idea why?
Thank you guys.
You need to tag the Label and ImageView.
tag means assign a integer value to label and ImageView and you can access using the tag.
You can assign tags in xib.
Then you can access the view like this[someController viewWithTag:(int)];
I think I have resolved my problem. I was trying to modify the views before adding them to the UIScrollView.
If I call my methods after the [scrollview addSubView:myView], everything works fine.
I created a subclass of an UIView called smartView.
Then I created a NSInteger parameter viewID.
Now in the IB I change the class of a standard UIView to my smartView.
My question is how can I supply a value for my viewID parameter in IB?
Is it possible? If not, is there another way besides "Tag" parameter to give a UIView component a unique id?
I think the way to supply that value in IB would be to create a IB Plug-in.
See here for a tutorial, and here for the Apple Reference Document.
If you provide an IBOutlet for your smartView in the view controller then you can access it like:
self.mySmartView.viewID = _viewID;
Perhaps there's a better way to set this up so I'm open to suggestions.
But here's what I'm doing. I have a main UIView. On top of that I have a UIImageView and another UIView. When the UIImageView changes, I want to change the second UIView. So I have a class for it and the IB object's type is set to the class. In the .m of that class is a drawRect method that draws some rectangles. Also in the .m is a NSMutableArray property that is synthesized. I created an instance of that class in the controller of the main view.
The problem: despite the fact that the drawRect works fine when the app starts (as traced in the debugger,) when the UIImageView changes I call a "setNeedsDisplay" on the instance variable of the second view after updating the #synthesize'd array but the drawRect does not get called.
I think it has to do with instances. I wouldn't think threading would be an issue here. I just want to draw in a separate area of the screen based on an image also displayed.
OK, for those who see this and are having a similar issue...
It was indeed an 'instance' problem. I had created the secondary UIView in IB. I think because I had established a connection there, the code ran fine on startup.
The fix was to create the subview programmatically and add it to the main view. That way there was only the one instance. Updating the #synthesize'd array variable and the subsequent calls to drawRect (via setNeedsDisplay) went to the one instance of the secondary view, the one I had added as a subview to the main one. Problem solved.
I am trying to create a custom UIView that is stored in a nib so that I can position it using Interface Builder.
I have created a class called RippleView that extends UIView. I'd like to position this view as a subview using Interface Builder. To do so, I've dragged a new view into my existing view in Interface Builder and gave it the RippleView class identity. I then linked my RippleView outlet to the view I just created.
In the RippleView class, I've implemented initWithCoder which doesn't do anything other than call [super initWithCoder...]. In awakeFromNib I go ahead and initialize my code.
Now, for some reason, if I try to check the bounds of the RippleView in awakeFromNib, I get ridiculous values (0 width and 1081171968 height). These bounds don't change in drawRect, so I don't think it's an issue of the view not being initialized. I get similar values in my touchesMoved event handler.
I had no problems when I was programmatically creating the subview (initWithFrame). What could be causing the frame bounds to go haywire?
Solved! The issue had nothing to do with the code. The UIView bounds are floats and I was printing them as integers. It's things like this that separate the programmers from the dropouts :p
Can you post your initWithCoder: implementation? If you're not setting the value of self, or if you've misspelled initWithCoder:, you might see these sorts of problems. Have you tried setting a breakpoint in initWithCoder: to make sure it gets called?