Change image after clicking a button

Discussion in 'iOS Programming' started by todizara, Dec 17, 2010.

  1. todizara macrumors newbie

    Joined:
    Dec 17, 2010
    #1
    Hi everyone!

    I'm starting developing for iPhone. I want to do a application with a button and when you click the button the picture in that button will change with another image.

    I use Interface Builder, and so I managed to place a button with a picture, the problem is to change image after clicking. I don't have any idea, could you help me?

    Thank you!
     
  2. JASApplications macrumors 65816

    Joined:
    Nov 24, 2010
    Location:
    Scarborough, United Kingdom
    #2
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    No. The Code Sharing forum is for promo codes and not application code. This thread as been moved to the proper forum: iPhone/iPad Programming. That is all.
     
  4. JASApplications macrumors 65816

    Joined:
    Nov 24, 2010
    Location:
    Scarborough, United Kingdom
    #4
    Oh yea. Sorry my bad!
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    So, now it's time to learn about IBOutlets, IBActions, setting property values, etc.
     
  6. JASApplications macrumors 65816

    Joined:
    Nov 24, 2010
    Location:
    Scarborough, United Kingdom
    #6
    Have you any idea of coding in Objective C or C? If you haven't the best thing you can do is buy a book and learn.
     
  7. todizara thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #7
    I happened to change the image of my button. At the first click, my function works fine, but the next click, the image changes just before the click and returns to the first image.

    I want that after clicking the image changes.

    Those are my codes:

    buttonViewController.h
    Code:
    //
    //  buttonViewController.h
    //  button
    //
    //  Created by SLVm on 18/12/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface buttonViewController : UIViewController {
    	UIButton* myButton;
    }
    - (IBAction)buttonClicked:(id)sender;
    @end
    
    
    buttonViewController.m
    Code:
    //
    //  buttonViewController.m
    //  button
    //
    //  Created by SLVm on 18/12/10.
    //  Copyright 2010 __MyCompanyName__. All rights reserved.
    //
    
    #import "buttonViewController.h"
    
    @implementation buttonViewController
    - (void)drawButton
    {
    	myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        myButton.frame = CGRectMake(80, 50, 70, 70); //set frame for button
    	
    	UIImage *buttonImage = [UIImage imageNamed:@"icon1.png"];
    	[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    	
    	[myButton setTitle:@"Ok" forState:UIControlStateNormal];
    	[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    	
    	[self.view addSubview:myButton];	
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];		
    	[self drawButton];
    }
    
    - (IBAction)buttonClicked:(id)sender
    {	
    	if (myButton.selected=YES)
    	{
    		UIImage *buttonImage = [UIImage imageNamed:@"icon2.png"];
    		[myButton setBackgroundImage:buttonImage forState:UIControlStateSelected];
    		
    	}else{		
    	UIImage *buttonImage = [UIImage imageNamed:@"icon1.png"];
    	[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    	}
    }
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    - (void)dealloc {
        [super dealloc];
    	[myButton release];	
    }
    
    @end
    
    
     
  8. JASApplications macrumors 65816

    Joined:
    Nov 24, 2010
    Location:
    Scarborough, United Kingdom
    #8
    This uses interface builder.

    .h
    Code:
    @interface ImagesViewController : UIViewController {
    	
    	IBOutlet UIImageView *userImage;
    	
    }
    
    @property (nonatomic, retain) IBOutlet UIImageView *userImage;
    
    - (void)updateImage;
    - (IBAction)buttonChangeImage:(id) sender;
    - (IBAction)btnBackImage:(id) sender;
    
    - (IBAction)back;
    
    @end
    
    .m
    Code:
    @implementation ImagesViewController
    
    @synthesize userImage;
    
    int y = 0;
    
    - (IBAction)buttonChangeImage:(id) sender {
    	y++;
    	if (y>8) y=0;
    	[self updateImage];
    }
    
    -(IBAction)btnBackImage:(id) sender {
    	y--;
    	if (y<0) y=8;
    	[self updateImage];
    }
    
    - (void)updateImage {
    	
    	switch(y) {
    		case 0: [userImage setImage:[UIImage imageNamed:@"1.png"]];
    			break;
    		case 1: [userImage setImage:[UIImage imageNamed:@"2.png"]];
    			break;
    		case 2: [userImage setImage:[UIImage imageNamed:@"3.png"]];
    			break;
    		case 3: [userImage setImage:[UIImage imageNamed:@"4.png"]]; 
    			break;
    		case 4: [userImage setImage:[UIImage imageNamed:@"5.png"]];
    			break;
    		case 5: [userImage setImage:[UIImage imageNamed:@"6.png"]];
    			break;
    		case 6: [userImage setImage:[UIImage imageNamed:@"7.png"]]; 
    			break;
    		case 7: [userImage setImage:[UIImage imageNamed:@"8.png"]];
    			break;
    		case 8: [userImage setImage:[UIImage imageNamed:@"9.png"]];
    			break;
    //etc
    
    }
    }
    
     
  9. todizara thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #9
    I used your code but nothing appears on the screen!
     
  10. JASApplications macrumors 65816

    Joined:
    Nov 24, 2010
    Location:
    Scarborough, United Kingdom
    #10
    I clearly stated "This Uses Interface Builder", did I not? So have you hooked IBActions and IBOutlets up in interface builder? If you don't have a clue what they are then go read a book on the iPhone SDK before attempting to build a App.
     
  11. todizara thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #11
    I use interface builder. I put a picture view, then I encode. le.m pm and then I made the connection between the image view with IBAction, but I don't have anny result
     
  12. JASApplications macrumors 65816

    Joined:
    Nov 24, 2010
    Location:
    Scarborough, United Kingdom
    #12
    userImage - UIImageView.
    buttonChangeImage - button1.
    btnBackImage - button2.

    And have you made sure your images are 1.png, 2.png, 3.png etc?
     
  13. todizara thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #13
    I see your point, but I want to make an application with one button.
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    Then the first thing to do is make the code work as given. Then after it's working correctly, you can figure out how to remove the extra button.

    In other words, you need to make it work, study it as it is, then change it.
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Your issue probably has to do with your logic surrounding the selected status of your button.
     
  16. flummoxed macrumors member

    Joined:
    Nov 27, 2010
    #16
    Yes, that was my first impression. I just learned this tonight playing with the button images. The "selected" state does not have any relation to button pressed. If you want the image changes to relate to touch events, then use the "highlighted" state.
     
  17. todizara thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #17
    Thank you for your answer, I'll see it
     
  18. todizara thread starter macrumors newbie

    Joined:
    Dec 17, 2010
  19. momolgtm macrumors newbie

    Joined:
    Nov 25, 2009
    #19
    Change image after clicking a button

    The above code is also a continuous loop that the button is pressed.
    I want to stand in the final image displays the picture button is pressed.
    How can I do?
    momolgtm
     
  20. todizara thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #20
    Could you explain more what do you want to do exactly?
     
  21. momolgtm macrumors newbie

    Joined:
    Nov 25, 2009
    #21
    Change image after clicking a button

    I pressed the button "buttonChangeImage"
    image 9. viewed...
    push the button again (buttonChangeImage)
    returning to image 1.
    I want to stop, image 9.
    My English is not very good ..
    sorry
    momolgtm
     
  22. todizara thread starter macrumors newbie

    Joined:
    Dec 17, 2010
    #22
    ok try this

    Code:
    -(IBAction)buttonChangeImage{
    	NSArray *image = [NSArray arrayWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg",@"image4.jpg",@"image5.jpg",@"image6.jpg", @"image7.jpg", @"image8.jpg",@"image8.jpg", nil];
    	
    	CGrect fram =CGRectMake(56, 426, 50, 30);
    	if(y<9){
    		UIImageView *myImage = [[UIImageView alloc] initWithFrame:frame];
    		[myImage setImage:[UIImage imageNamed:[image objectAtIndex:i]]];
    	}else{
    		UIImageView *myImage = [[UIImageView alloc] initWithFrame:frame];
    		[myImage setImage:[UIImage imageNamed:[image objectAtIndex:8]]];
    	}	
    }
    
     

Share This Page