Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

todizara

macrumors newbie
Original poster
Dec 17, 2010
29
0
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!
 
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!

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.
 
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.

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
 
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

}
}
 
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.
 
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.

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
 
I see your point, but I want to make an application with one button.

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.
 
Your issue probably has to do with your logic surrounding the selected status of your button.

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.
 
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
 
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
Could you explain more what do you want to do exactly?
 
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
 
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
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]]];
	}	
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.