PDA

View Full Version : Custom buttons iPhone app




warrmr
Aug 10, 2010, 05:17 PM
I am very new to programming for the iPhone and with C and obj-C. I have decided to jump straight in feet first and build my first app. As i get sidetracked very easy if i Don't have a project to work with or a form of focus.

My app is essentially a flashlight app that turns the LED on the iP4 on and off.

So far I have created the basic code that makes that work and it is linked to a
- (IBAction) button. This then depending on the state of the flash calls a method to turn the flash on or off when pressed. That is all well and good but doesn't look very pretty.

So the next logical step is to build the interface and make the app look good. So I have assigned a custom image to the button. It is behaving like it should but not how I want it.

My current button code looks like

-(IBAction) Button {
if (flashState == NO)
[self LightOn];
else
[self LightOff];
}


What i want is something that goes like this


-(IBAction) Button {
If (flashState == NO)
Button = light.png;
[self LightOn];
Else
Button == dark.png
[self LightOff];
}


Now i know the Syntax on that second lot of code is all screwy but it is more to show the logic of what I want.

I thank you in advance for all replys, But can I add that as i want to lean please dont just post a code snippet and say there you go because i will never learn. What i would like is either a poke in the correct direction or This is what you need to do and this is why.

I appreciate that I have a long way to go and need to grasp the basics as i am finding the syntax very difficult coming from VB6 when i was at college.



pflau
Aug 11, 2010, 12:43 AM
Do a search for setBackgroundImage:forState:

This allows you to set the image for the button on the fly. Make sure you set it for the right State.

warrmr
Aug 11, 2010, 06:28 PM
Thankyou good sir. That worked beautifully.



if (flashState == NO){
[self LightOn];
UIImage *btnImg = [UIImage imageNamed:@"PowerButt.png"];
[myButton setBackgroundImage:btnImg forState:UIControlStateNormal];
}
else{
[self LightOff];
UIImage *btnImg = [UIImage imageNamed:@"PowerButtRed.png"];
[myButton setBackgroundImage:btnImg forState:UIControlStateNormal];
}