Generate iPhone UIButton from parsed xml file - iphone

I have been able to NSXML parse an xml file from my server which loads a series of images and titles into a ScrollView...
But I can't find any reference of how to dynamically generate and position several UIButtons (or Alerts) on one of the UIViews.
Some of the images will need hotspots which when pressed opens an alert view with reference text.
Is it possible to create UIButtons from a parsed xml document? Thought I'd ask before spending any more time on chasing rainbows :)
Of course any pointers or URLs would be a bonus.

You can create an UIButton the same way you create any other ui elements:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake( 12.0f, 12.0, 50.0f, 30.0f)];
[button addTarget:self action:#selector(buttonPreseed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubView:button];
[button release], button = nil;

Related

UIButton with custom Type seems not to work properly

something strange happened during the development of my app. I have a view with several UIButtons in it. All are using custom artwork and are UIButton with CostumType.
For me everything felt right. In the simulator and on the phone.
But when I give the app in someone else's hand the person taps on a button and it won't work. It feels like the button is just reaction to a certain tap which in fact doesn't feel right (if you compare it to normal behavior).
What can I do to make it behave normal? Normal means that somebody who is used to iOS Apps can use it like he except it works.
Here is an example code for a button:
focusButton = [UIButton buttonWithType:UIButtonTypeCustom];
[focusButton setFrame:CGRectMake(self.bounds.size.width-(self.bounds.size.width/widthFactor)+108, self.bounds.origin.y+(self.bounds.size.height/(heightFactor*2))+2, 36, 40)];
focusButton.contentHorizontalAlignment = false; // I think these lines doesn't effect the behavior
focusButton.contentVerticalAlignment = false;
[focusButton setBackgroundImage:[UIImage imageNamed:#"arrowUp.png"] forState:UIControlStateNormal];
[focusButton setBackgroundImage:[UIImage imageNamed:#"arrowUpH.png"] forState:UIControlStateHighlighted];
[focusButton addTarget:self action:#selector(focusOrDefocusCourse) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:focusButton];
The Button Background looks like this:
this is woking fine.. Once just test with the frame..
UIButton *btn_foucs = [UIButton buttonWithType:UIButtonTypeCustom];
[btn_foucs setFrame:CGRectMake(20, 20, 200, 40)];
[btn_foucs setImage:[UIImage imageNamed:#"btn_erase.png"] forState:UIControlStateNormal];
[btn_foucs setImage:[UIImage imageNamed:#"btn_erase_h.png"] forState:UIControlStateHighlighted];
[self.view addSubview:btn_foucs];
You may like to take a look at the UIButton property imageEdgeInsets. This allows you to make the image draw into only part of the frame, so that the touchable area is bigger than the image itself (reducing the chance of 'missing' the button). You could do the following, for example:
int touch_offset = 10;
[focusButton setFrame:CGRectMake(self.bounds.size.width-(self.bounds.size.width/widthFactor)+108-touch_offset, self.bounds.origin.y+(self.bounds.size.height/(heightFactor*2))+2-touch_offset, 36+(touch_offset*2), 40+(touch_offset*2))];
focusButton.imageEdgeInsets = UIEdgeInsetsMake(touch_offset, touch_offset, touch_offset, touch_offset);
This should make the touchable area 10 pixels wider than the image on each side, adjustable by changing the touch_offset value. As a general guideline, Apple recommend using touchable areas no smaller than 44x44 pixels.

How to draw small button inside big button in iOS

I made a button already. When I click it, it becomes big but letter is not in good position. I want to put another button inside the big button. How can I do this ?
If you just wish to add a second button as a subview of the first button, you can use this:
UIButton *mySmallButton = [[UIButton alloc] initWithFrame:CGRectMake:(0, 0, 50, 50)];
[mySmallButton setBackgroundColor:[UIColor redColor]];
[myBigButton addSubview:mySmallButton];
However, if you're just looking to modify the text size/alignment of the first button once it has grown you can use these:
[[myBigButton titleLabel] setTextAlignment:UITextAlignmentCenter];
[[myBigButton titleLabel] setFont:[UIFont systemFontOfSize:24]];

Where to insert code that creates buttons programmatically?

I receive some JSON from the net and based on that data, I must create 2 or 3 buttons. Part of my gui is static and created in NIB (won't change), only the number of buttons will change. I found this code for making buttons in code:
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
Is this the right way? In which method of my viewcontroller should I put this code?
You can add the button whenever you want, as long as the view has been loaded already. The one thing you'd need to add to the above code is
[[self view] addSubview:button];
Using this code, you have a button on the screen, but it won't be able to trigger any actions. You'll probably also want to add:
[button addTarget:self action:#selector(someMethod:) forControlState:UIControlEventTouchUpInside];
You should add the buttons in the delegate/method that parses the JSON data.
Don't forget to add the created buttons to your view:
[containerView addSubview:button];

how can i create array of buttons

hi i am new to iPhone.what i did is creating a 4 buttons individually.i need button tag values.when ever i check it in console i got 0 for 4 buttons,because i create 4 individual buttons .But i need buttons tag values like for first button, tag value 0,for second button, tag value 1.... like this how can i done this pls post some code.Thank u in advance.
for(int i=0;i<3;i++){
UIButton *theButton=[[UIButton alloc]init];
theButton.tag=i;
//set their selector using add selector
[theButton addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchDown];
//set their frame color title or image
}
-(void)buttonClicked:(UIButton *)inButton{
int tags=inbutton.tag;
}
You can specify the button tag like this :
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTag:1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTag:2];
By default the tag of the button is zero So if you have created four individual buttons the tag for all will be zero So What you can do is
If you have added four buttons from the xib file then set their tag as per your requirement in the xib file itself and give them same name
If you have taken the four buttons through code then set the tag through code
//Alloc buttonName1
buttonName1.tag=0;
//Alloc buttonName1
buttonName1.tag=1;
//Alloc buttonName1
buttonName1.tag=2;
//Alloc buttonName1
buttonName1.tag=3;
And for using it you have to go with pawans answer.
hAPPY cODING...

Mail app up/down arrows

Is there a way to get access to the up/down arrows used in the Mail app and implement them the same way?
No, there's no supported way to do it. I'd suggest you file a bug on Radar asking Apple to include more built-in artwork. In the mean time, you'll just have to draw your own.
Can you clarify your question?
The graphics for up/down are not easily accessible, though you can probably find a way to get them. For instance, see http://0xced.blogspot.com/2009/04/extract-uikit-artwork.html.
But if you're looking for the behavior, it's really quite simple. The arrows are on a momentary UISegmentedControl. Attach to valueChanged: to tell when the user taps on one of them, and use selectedSegmentIndex to tell which one they tapped.
Nowadays they are part of the sample code of IOS 4.1. Check the NavBar sample.
As Steven Fisher said, you can’t access the Apple graphic easily. But they are relatively easy to remake.
Given the images I had some trouble making this look right in iOS 7 where a UISegmentedControl will not work. This code did the trick however (added to viewDidLoad in the ViewController).
UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom];
[upButton setImage:[upImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
forState:UIControlStateNormal];
upButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 6);
[upButton sizeToFit];
UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
[downButton setImage:[downImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
forState:UIControlStateNormal];
downButton.contentEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
[downButton sizeToFit];
UIBarButtonItem *upButtonItem = [[UIBarButtonItem alloc] initWithCustomView:upButton];
UIBarButtonItem *downButtonItem = [[UIBarButtonItem alloc] initWithCustomView:downButton];
[upButton addTarget:self action:#selector(YOUR_UP_SELECTOR:) forControlEvents:UIControlEventTouchUpInside];
[downButton addTarget:self action:#selector(YOUR_DOWN_SELECTOR:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItems = #[downButtonItem, upButtonItem];