picture button?

Discussion in 'iOS Development' started by kyle1320, Jul 30, 2010.

  1. kyle1320

    kyle1320 Banned

    Joined:
    Feb 5, 2009
    Messages:
    153
    Likes Received:
    0
    Device:
    2G iPod touch
    so, im just learning how to program and its probably right in front of my face, but how do i use a picture as a button in xcode/interface builder? i have a UIImage button1 linked to a UIImage view and an IBAction buttonWasPressed, but how do i link the image view and buttonWasPressed? or do i have to do it some other way?
  2. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    An ImageView is an ImageView, not a button. Use a UIButton, and set the background image, or regular image, depending on what you would like to do.
  3. SpeedyApocalypse

    SpeedyApocalypse Member

    Joined:
    Dec 27, 2009
    Messages:
    564
    Likes Received:
    13
    Device:
    4G iPod touch
    To use it as a button, here is what I did for my upcoming game that is currently in development. It takes the finger position and sees if it is over the UIImageView.

    [OBJC]-(void)touchesBegan

    Please Register or Log in to view images

    NSSet *)touches withEvent

    Please Register or Log in to view images

    UIEvent *)event {

    UITouch *touching = [[event allTouches] anyObject];
    CGPoint location = [touching locationInView:touching.view];

    if (CGRectContainsPoint(yourImage.frame, location)) {

    //Do Stuff

    }
    }
    [/OBJC]
  4. kyle1320

    kyle1320 Banned

    Joined:
    Feb 5, 2009
    Messages:
    153
    Likes Received:
    0
    Device:
    2G iPod touch
    thanks, i just made it a button xP but now im trying to make the background picture of the button change when i press the button. so i thought id make a function that would change it whenever i press the button. i can link the function to the button, but how do i make it change the button background? i cant make the button an IBOutlet.

    EDIT: nevermind, i can make the button an IBOutlet but what do i put in the code to make it change the background?
  5. startrees

    startrees Member

    Joined:
    Nov 29, 2008
    Messages:
    223
    Likes Received:
    0
    Device:
    iPad 2 (White)
    that is how you change it. Where it says BUTTON place the name of your IBOutlet for your button.
  6. kyle1320

    kyle1320 Banned

    Joined:
    Feb 5, 2009
    Messages:
    153
    Likes Received:
    0
    Device:
    2G iPod touch
    im creating multiple buttons. how do i do that but use the sender (whichever button calls the function)? sorry im new to developing

    Please Register or Log in to view images



    EDIT: anything wrong with this?:

    Code:
    - (IBAction)buttonWasPressed:(id)sender
    {
        setBackgroundImage:(sender);@"light_on.png"; forState:UIControlStateNormal;
    }
  7. SpeedyApocalypse

    SpeedyApocalypse Member

    Joined:
    Dec 27, 2009
    Messages:
    564
    Likes Received:
    13
    Device:
    4G iPod touch
    Do you mean change the image-button, or an actual UIButton. If it is a UIButton, do what StarTrees said above. You can also change it for when it is pressed or disabled by adjusting the UIControlStateNormal.

    If you want to change an image, do the following:

    [OBJC]myImage.image = [UIImage imageNamed

    Please Register or Log in to view images

    "myImage.png"];[/OBJC]
  8. kyle1320

    kyle1320 Banned

    Joined:
    Feb 5, 2009
    Messages:
    153
    Likes Received:
    0
    Device:
    2G iPod touch
    i want to change the background image of a UIButton, but i want to do this for multiple buttons so i want it to set the background image for whichever button i press
  9. startrees

    startrees Member

    Joined:
    Nov 29, 2008
    Messages:
    223
    Likes Received:
    0
    Device:
    iPad 2 (White)
    I think he wants to make it a function so he can use it a bunch

    You do this

    Then when you want to call it
    I suppose your doing this in the same file

Share This Page