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
Related
how do I ensure a custom UIView I have is repositioned properly after an orientation change of the iPhone?
Background
As a parent view I have a UIViewController with an XIB
I have a custom UIView which is used in this parent (inserted in the XIB via Interface Builder). It programmatically at init creates some UIImageView's and adds them via "self addSubview", and then positions them via "self addSubview:imageView1" type approach
So the questions are how do I ensure I get the correct layout of these as orientation changes. Specifically:
Where do I trigger any such redrawing of the custom UIView? (e.g. is there a method in the parent UIViewController I should be using to trigger from?)
When the trigger occurs how do I request the redraw of the custom UIView? Do I need a custom method I create myself like "relayoutCustomView"? or should I be able to use an existing method in the custom UIView, in which case do I need to make sure in the custom UIView that my layout code is in a particular method?
thanks
how's this for the answer - seems to be starting to work, not sure if it's the best way:
separate out the layout code in my custom view and put in a separate method and make this public
this method will be called from within the custom view "init" method upon setup
also now however create an "didRotateFromInterfaceOrientation" method in the parent, and from within here also call into the new custom view layout method mentioned in [1]
noted it had to be "didRotateFromInterfaceOrientation", as with the "didRotateFromInterfaceOrientation" method it didn't work as the self.bounds result hadn't yet changed
How does this sound? I didn't use the "setNeedsDisplay" anywhere...
I need to implement a scrolling window in order to accommodate all the items on the form I am creating. My current implementation is a UIViewController (that's vcAddCourse) and it has a UIView in it with my current form.
Here is what I have done to add the UIScrollView into the equation.
1) Using IB, I dragged a uiscrollview object 'into' the existing uiview object.
2) Using IB, Ctrl-dragged from file owner to the IBOutlet I created for the new uiscrollview
Here is how I init this controller.
3) I made sure that all the items on my form were now dragged to be under the UIScrollview object.
Finally, over in my .m file, in the ViewdidLoad() function, I added the following line:
theScroller.contentSize=CGSizeMake(328, 680);
No joy. I see the form but it does not scroll.
Note: this on iphone simulator.
What else must I do to swap out the UIView for the new UIScrollView?
Thanks!
To keep things like these clear, I keep the scrollView and the (larger) subview separate in the xib file (as siblings). Your form should be on a UIView, and would be larger than the scrollview. That way you can use IB to layout the form exactly as you want.
To make things work you just have to have the IBOutlets to the UIScrollView and the UIView, and add the view to the scrollview in the viewDidLoad method of your viewController:
[theScroller addSubView:formView];
I'm not exactly sure, but I don't think you have to set the contentSize manually after this, as it should automatically be made big enough to fit your view. If it's not working you might try this:
theScroller.contentSize = formView.frame.size;
Hope this helps
I have a custom UIView which is a sub-view of a UITableViewCell. When the cell is drawn, so is my custom view, correctly calling drawRect: in my UIView sub-class. When the cell is reloaded, the view is drawn correctly (drawRect: is called).
When a certain event happens (eg timer), I want to redraw the view without having to reload the whole cell. (This is not the only view in the cell.)
However, when I call setNeedsDisplay on my sub-view, nothing happens.
I'm guessing that when my view's drawRect: is called, the resulting image is cached in a backing somewhere such that when I call setNeedsDisplay that backing is redrawn to the screen, but my drawRect: call is never called again.
Why is drawRect: never called again after the cell is created?
EDIT: The cell's subviews are created from from a nib. ie: a UIViewController is loaded from the nib and its view is added to the UITableViewCell.
I made a test case from scratch with a custom view that isn't loaded from a nib (created by code) and it works as expected: setNeedsDisplay results in a drawRect: call. It must be something that's either set up in the .xib file or something that happens differently to a view when it's loaded from a nib.
I just ran into the same problem myself. For me it was an issue with my XIB - I am subclassing a UIView though - so adapt accordingly. My mistake in IB was that I attached my controls to the "file's owner" instead of attaching them to the UIView. I'm sure that in my inexperience there is something larger going on - perhaps someone with a bit more experience can elaborate.
Hope that helps!
I have a beginner iPhone project going. I have a TableView, which is just a listbox with a bunch of entries and a small UiView on the bottom. See the pic.
The problem is that the UiView essentially becomes part of the listbox and will only appear when I scroll down to the last item in the TableView.
I'd like the UiView to be anchored on the bottom and never move. I've looked all around the Interface Builder and can't find anything (though in its defence I am a total noob to iPhone dev). How do I accomplish a trick like this?
alt text http://www.sqleffects.com/mystuff/ibissue.jpg
Ok, what you need is a UIView, that contains
an UIView, which acts as the TableViewController and thus has to implement the UITableViewDelegate and UITableViewDataSource protocols (but it MUST NOT be derived from UITableViewController directly, since by doing so the UITableView will automatically take up the whole screen size (except toolbars, navbars and/or tabbars))
another UIView, the one you would like to place at the bottom
by doing so, you can create an UITableView in IB (not an UITableViewController!) and connect it with the UITableView property in your UIView (the one mentioned in 1.)
using this method it is possible to give the UITableView a fixed size (which you'll need to, to have room at the bottom for your second UIView)
To build off gabtub's answer, the UIView containing your table view and your bottom view doesn't need to implement the UITableViewDelegate and UITableViewDataSource methods.
Since it sounds like you're building with a view controller, I'd make your main view controller a subclass of UIViewController (instead of using UITableViewController). You could then add your UITableView and your UIView the the UIViewController's view instance.
I'd then make your UIViewController subclass implement the UITableViewDelegate and UITableViewDataSource protocols - you'll end up with something that looks similar (code-wise) to your old UITableViewController subclass, but it's view property will be the underlying UIView instead of UITableView instance (if you poke around in the debugger, you can see the [UITableViewController view] and [UITableViewController tableView] return the same object)
One of the advantages over gabtub's suggestion is it saves you from creating a one-off UIView subclass, since you've probably already got a one-off UIViewController subclass (or, previously had a one-off UITableViewController subclass).
The problem is probably, that right now, the UIView is a section footer view, and is therefore contained within the table view.
If you delete the UIView from the TableView, scale the TableView to make space for the UIView and a bit more and put your UIView where you want it, it probably will be added to the TableView's superview. You can then resize the TableView to the correct size.
The bottom line is this: if you create your project based on the Navigation template, you can't resize the UiTableView, period.
So to do what I want, I basically have to start from scratch and pick a View or Window based template and drop the UiTableView on it manually.
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?