I am quite new to the whole IPhone development thing and was playing around with the Three20 library. The samples for this library showed me an easy way to create a nice looking button with styles etc and this to my view:
TTButton* button = [TTButton buttonWithStyle:#"forwardActionButton:" title:#"Login"];
[button setFrame:CGRectMake(245, 160, 65, 33)];
[self.view addSubview:button];
Works great. Now I have created a nib file in interface builder with my whole view layed out including all sorts of textfields etc.
What I would like to do is, is to create a placeholder for a button in Interface Builder and in my viewDidLoad method replace this placeholder with the actual TTButton instance. This would prevent my from having to call setFrame with an hardcoded location and instead make the whole interface design process much more free.
I have already tried creating a UIView object in interface builder, changing the type to TTButton and then assigning a new instance but seems to put the new button at coordinates location 0,0.
Can anybody point me in the right direction on how to achieve this?
waseem,
Unfortunately three20 does not use interface builder at all. The best thing you can do is layout a mock-up view and grab button x-y locations from that, then use those to update your TTButton locations in code.
Related
I have defined a UIView in the header file named rectangle0
I got a question in regards to IB vs coding a view. Please see the following code example:
rectangle0.frame = CGRectMake(0.0, 70.0, 320.0, 190.0);
Now I can re-create the look of the view easy enough in IB except for the coordinates which are greyed out.
So by calling this code in an IBAction after designing the UIView in IB I can display in the app.
[self.view addSubview:rectangle0];
Now the last line of code will call this view to appear at the set coordinates if I code it - but if I build in interface builder I can make it look exactly the same, but it appears from Y coordinate 20 (just under the status bar) and I can't set the coordinate upon calling it - is there a way to do this??
The UIView is being build outside of the ViewController and then linked to it in the connections inspector.
cheers Jeff
I have added an image to show what screen I am using - as you can see the x and y coordinates are greyed out, it would be great if I can set those to something else.
A view inside the NIB is just an object like any other. It is made to be reusable and I believe that is the key point that resulted in the coordinates being greyed out.
Greying out the position makes the developer worry about where is the view going to be placed. I say that because the position is pretty dependent on the container that your view object is going to be added to.
Although it is just my personal opinion and I am looking forward to hearing if there is indeed a way to do that.
You (effectively) said the view is managed by a view controller. Before you can resize the view, you need to change the Size setting in the view controller's Simulated Metrics to "Freeform". (It is probably set to "None" by default.)
I'm new to objective C, I have a view based application which communicates with a web service and recives an xml, parse it and map its contens an appropriate view component (e.g if thats a date show the question with datepicker, if question has 2 values show it with a segmented control, if more with a pickerview..etc) so its a dynamic questionary with many pages filled with multiple components.
how can I do the design for the mapping the xml response into related UI view components on runtime? I want to make the code as object orient as possible and not too many if elses in my view controller class, but instead it should only receive a let's say "uiview object" filled with necessary view components and it will just show it. I have a UiScrollView and want to add new different approprite Uicomponents(Picker, textbox, label..) to it on real time, How can I do this? I think in the Interface builder I only need to put the scrollview and the rest will be in code? and using a scrollview a good choice here? thanks.
[self.view addSubView: imageview];
[self.scrollView addsubview:pickerview];
[self.view addSubView:scrollview];
like wise the custom view may be like uibutton,textfield,textview,imageview
you can't do without a UIScrollView. you have to define the set of elements you wan to inject into your view and make case statement for the xml data you are receiving in your receiving data handler.
If you are new to Objective-C (i.e. CocoaTouch) and if your polls are fixed enough , maybe you could try setting up the differents views in the code and the data you are retrieving from the net will be to decide which view to display.
another tip will be to subclass UIKit elements to init them from a XML node.
I generally don't use Interface Builder (I hate it). But I am being forced to use it, because I was invited to a project where people are using it. I am trying to create a custom UISlider.
I have created a UISlider custom class, with my own images for the slider parts.
If I add a new object to my main code using
mySlider *one = [[mySlider alloc] initWithFrame:CGRectMake(0,0,60,30)];
I see the slider as I created, beautifully.
But if I use this class on interface builder, to change the appearance of a UISlider I create there, the slider continues to have the same appearance as before and when I run the app the interface shows the slider with the default appearance, not the one I designed.
I created the slider on IB, simply dragging a UISlider on the interface and changing its class to the one I've created. Is there something else that has to be done? Why is it not showing as defined on the custom class?
thanks
You need to implement the initWithCoder: initializer. initWithFrame: will not be called when a nib is loaded.
I'm pretty sure I saw an example where the graph wasn't filling the whole iPhone screen, but I can't get that to happen in my app, nor in the Core-Plot Test app from Switch On The Code.
I've added a subview to the original CPLayerHostingView in the sample, then changed the classes – original back to UIView, new subview to CPLayerHostingView, and I've reconnected the File's owner's view outlet to the new subview.
When I create a graph with:
graph = [[CPXYGraph alloc] initWithFrame: theSubviewOutlet.bounds];
… and step through the first stages of building up the layers the bounds are accurate (i.e. the same as in the .xib)
however, when all the initialization is done, and the graph shows up, it fills the whole superview.
Am I missing something obvious?
These types of questions are better asked on the core plot mailing list, because we may miss them over here.
There is nothing special about a CPLayerHostingView. You should be able to add it as a subview to your UIView, and resize it as you wish. You should also be able to set springs and struts in interface builder, or via code.
Perhaps you have your parent UIView setup to resize subviews in some way? In any case, Core Plot should not be doing anything to modify the host view frame.
I wanted a control like the UISlider, but with two thumbs instead of one so you could specify a range instead of just one value. I read up on other people looking for the same thing and suggestions about making new controls were over my head.
So I decided to setup what I wanted in a nib and then created a view controller to handle its behavior. It actually works really well this way accept for 2 things.
1) To load my 'custom control' into another nib I need to do it through code like this...
MaxMinSliderViewController *feeMaxMinSlider =
[[MaxMinSliderViewController alloc] init];
[self.view addSubview:feeMaxMinSlider.view];
feeMaxMinSlider.view.frame = CGRectMake(20, 20, 280, 54);
...instead of dragging and dropping into other nibs like normal IB controls. Is there an easy way to be able to do this?
2) In all my reading about doing this, its seems as though I was supposed to (to do it the right way) subclass UIView instead of UIViewController. I couldn't setup the UIView correctly and get it working though.
Thoughts?
instead of dragging and dropping into
other nibs like normal IB controls. Is
there an easy way to be able to do
this?
Nope! There's only an incredibly hard way!
Assuming you manage to convert your class into a custom UIView instead of a UIViewController you can do part of this quite easily within Interface Builder (depending upon what kind of init method your custom control has).
Within Interface Builder drag and drop a plain old UIView from the Library Window onto your view and position/size it as required.
Then with the UIView selected, switch to the Identity tab of the Inspector Window (the tab with a little (i) icon on it) and change it from UIView to the name of your custom class. This causes an instance of your custom class to be instantiated when the NIB is loaded, instead of an empty UIView.
You can then hook up the view to an IBOutlet etc as normal.
The advantage of this approach is the ability to position and size your control visually etc. It does however only appear as a rectangle, and you don't get the ability to graphically configure the additional properties of your control as you would with a UIButton or similiar control which Interface Builder has additional support for.
I find this technique great, as I'm not that much of a visual thinker and find it hard to picture in my mind how things will look when I read a section of code containing lots of frame = CGRectMake(20, 20, 280, 54) etc. Being able to do it visually with a mouse is much better for me.
There is an Interface Builder Plug-in Programming guide, but it only mentions Mac OS X, not the iPhone.
Also, as of XCode 3.2.1, there is only a new project template for creating a Mac OS X IB Plug-in; there is no such project template for iPhone (Cocoa Touch). So my guess is this is not applicable for the iPhone.