how to change image in imageView on button click - iphone

I want to change the image in image view on button click but it does not changing. I am using JPG image to load in image View
- (IBAction) changeImage:(id)sender
{
UIButton *tappedButton = (UIButton*) sender;
NSInteger selectedIndex = [tappedButton tag];
if (selectedIndex==0)
{
UIImage *modelimage = [UIImage imageNamed:#"compositone.jpg"];
modelImagewView.image=modelimage;
}
else
{
UIImage *modelimage = [UIImage imageNamed:#"composittwo.jpg"];
modelImagewView.image=modelimage;
}
}

Try this format - modelImagewView.image = [UIImage imageNamed:#"composittwo.jpg"];
Also, make sure you have set tag numbers properly so that selectedIndex is not always 0. At the same time check if you have linked this IBAction to the appropriate button.

Try This Code
- (IBAction) changeImage:(id)sender
{
UIButton *tappedButton = (UIButton*) sender;
if (tappedButton.tag==0)
{
modelImageView.image=[UIImage imageNamed:#"compositone.jpg"];
}
else
{
modelImageView.image=[UIImage imageNamed:#"composittwo.jpg"];
}
}

Related

Hide button on one touch itself

i want to hide button on one touch itself. when the user touches the button that button should get hide but on one touch.The buttons are moving randomly. The moving buttons shoul get hide on touch. i have done it but after two three times pressing only it gets hide. i am using touchupinside event. Can anyone help me?
-(IBAction)clickButton1:(id)sender
{
if (button1.tag==1)
{
button1.hidden=TRUE;
}
else
{
button1.hidden=FALSE;
}
}
-(IBAction)clickButton2:(id)sender
{
if(button1.hidden==TRUE && button3.hidden==FALSE)
{
button2.hidden=TRUE;
}
else
{
button2.hidden=FALSE;
}
}
Thanks in advance
Replace your code with this Button touchUpInside
-(IBAction)hide:(id)sender
{
UIButton *tmp = (UIButton *)sender;
tmp.hidden = YES;
}
Replace your first IBAction method with this one:
-(IBAction)clickButton1:(id)sender
{
UIButton *button1 = (UIButton *)sender;
if (button1.tag==1)
{
button1.hidden=TRUE;
}
else
{
button1.hidden=FALSE;
}
}
You can create your button on viewDidLoad with loop
-(void) viewDidLoad{
for ( c = 0; c < 10; c++ ){
Buttons[c] = [[UIButton alloc] init];
Buttons[c].tag = c;
}
}
after that you can control the show hide with following code.
-(IBAction)yourActionMethod:(id)sender
{
//your normal action codes here
UIButton *tmp = (UIButton *)sender;
if (tmp.tag == 0) {
// some codes
} else {....}
//control buttons of the
    for (int i = 0; i < tmp.tag; i++){
Buttons[i].hidden = yes;
}
   
}

Changing UIImageView with button

I am wanting to press a button and alternate between two images with one UIImageView.
Right now when I run it, I press the button, the image changes but will not change back. What do I need to change in this action code to make it alternate?
- (IBAction)Change:(id)sender {
image.image = [UIImage imageNamed:#"car.png"];
}
Try following code :
- (IBAction)Change:(id)sender {
if ([sender isSelected])
{
imageView.image = [UIImage imageNamed:#"car.png"];
[sender setSelected:NO];
}
else
{
imageView.image = [UIImage imageNamed:#"bike.png"];
[sender setSelected:YES];
}
}
To Display sequence image on button click.
.h file :
int counter;
.m file :
in viewDidLoad initialize counter = 0
Then
- (IBAction)Change:(id)sender {
counter++;
imgView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png",counter]];
}
And give your image name like 1.png, 2.png, 3.png and so on...
You have to maintain a flag.
In Somewhere before tapping the button, assign the flag
BOOL isFirstImageShown=YES;
Then on your button action
- (IBAction)Change:(id)sender {
if(isFirstImageShown)
{
isFirstImageShown=NO;
yourImageView.image = [UIImage imageNamed:#"yourSecondImage.png"];
}
else
{
isFirstImageShown=YES;
yourImageView.image = [UIImage imageNamed:#"yourFirstImage.png"];
}
}
You want that on button click you need another image you have to first set the image when you load the view and then on button click.

Trouble in making hidden button Visible

Following code i am writing to hide some buttons in viewDidLoad. Here Buttons Are hiding
- (void)viewDidLoad
{
for (int i = 100; i<117; i++)
{
UIButton *smileyButton = (UIButton *)[scroll viewWithTag:i];
UITextField *smileyFields = (UITextField *)[scroll viewWithTag:i];
UIImageView *smileyImage = (UIImageView *)[scroll viewWithTag:i];
smileyFields.hidden = YES;
smileyButton.hidden = YES;
}
}
Now in Following Action am making Buttons Visible. But buttons are not Visible
-(IBAction)editButton:(id)sender
{
for (int i = 100; i<117; i++)
{
UIButton *smileyButton = (UIButton *)[scroll viewWithTag:i];
UITextField *smileyFields = (UITextField *)[scroll viewWithTag:i];
UIImageView *smileyImage = (UIImageView *)[scroll viewWithTag:i];
[smileyFields setHidden:NO]; //TextFields Not Visible
[smileyButton setHidden:NO]; //Buttons Not Visbile
}
}
If you have several views with the same tag, function viewWithTag will return only one view, so if you call this 3 times, you get always the same view.
To do what you want, you could iterate all subviews and check tags:
for (UIView *aView in scrollView.subviews) {
if (aView.tag >= 100 && aView.tag < 117) {
aView.hidden = NO;
}
}
Are you building the view controller in Interface Builder? If so, set the Tag of each thing you want to hide to a different number: try something simple like 1, 2, 3, etc. If you're building in code set the tag property instead. Remember your maximum tag number (let's assume it's 4).
Then add the following to your .h:
- (void)setTaggedViewsHidden:(BOOL)hidden;
and the following to your .m:
- (void)setTaggedViewsHidden:(BOOL)hidden {
for (NSInteger tag = 1; tag <= 4; tag++) {
[scroll viewWithTag:tag].hidden = hidden;
}
}
In your viewDidLoad call it like so:
[self setTaggedViewsHidden:YES];
and in your editButton: selector call it as:
[self setTaggedViewsHidden:NO];
Remember to adjust the code in setTaggedViewsHidden to match the tags you're using. The best way to do this is to #define a constant for the min and max tags and use those in the for loop.

Unable to keep UIButton in selected state after TouchUpInside Event

I have the need for an UIButton to maintain a pressed state. Basically, if a button is in a normal state, I want to touch the button, it highlight to its standard blue color and then stay blue after lifting my finger.
I made the following UIAction and connected the buttons Touch Up Inside event to it.
-(IBAction) emergencyButtonPress:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
But what happens, after I remove my finger, the button goes back to a white background. For a test I added a UISwitch and have it execute the same code:
-(IBAction) emergencySwitchClick:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
In this case the button toggles to a highlighted and non-highlighted state as I would expect.
Is there another event happening after the Touch Up Inside event that is resetting the state back to 'normal'? How do I maintain the highlighted state?
The highlighted state is applied and removed by iOS when you touch / release the button. So don't depend on it in your action method.
As one of the other answers says, set the highlighted image to be the same as the selected image, and then modify your action method:
-(IBAction) emergencyButtonPress:(id) sender
{
emergencyButton.selected = !emergencyButton.selected;
}
If you want something like a toggle button, customize your "selected" state to be your "pressed" state, and then write something like this:
- (IBAction)buttonTapped:(id)sender {
[(UIButton*)sender setSelected:![sender isSelected]];
}
if you want the above code to work, you should set an image for the state selected and one (even the same) for he state highlighted.
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateHighlighted];
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateSelected];
hope it helps.
I had this same problem, and this is what worked for me:
-(IBAction)buttonPressed:(id)sender {
if (isSelected) {
[_button setSelected:NO];
[_button setImage:[UIImage imageNamed:#"not_selected.png"] forState:UIControlStateSelected];
isSelected=NO;
}
else {
[_button setSelected:YES];
[_button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
isSelected=YES;
}
}
This is not possible unfortunately, as soon as you let go Apple changes the state again.
One option I've used before (but not pretty) is that you use a performSelector with delay 0.1 after the button is pressed to put it again in the selected state. This did work in my case.
EDIT: If you don't want to see the blinking effect, just set the delay to 0.0
This worked for me:
- (void)tapButton:(id)sender{
UIButton *button = sender;
if (!button.selected){
[self performSelector:#selector(highlight:) withObject:button afterDelay:0.0];
}else{
[self performSelector:#selector(removeHighlight:) withObject:button afterDelay:0.0];
}
}
- (void)highlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = YES;
}
- (void)removeHighlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = NO;
}
Try this simple answer....
- (void)mybutton:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = ![button isSelected]; // Important line
if (button.selected)
{
NSLog(#"Selected");
NSLog(#"%i",button.tag);
}
else
{
NSLog(#"Un Selected");
NSLog(#"%i",button.tag);
}
}

Iphone : Applying strechable images to a button disables it

I am sure I am doing something stupid here. I build a category on top of UIButton which I want it to take all of the background images assigned to it (different states) and convert them to stretchable versions and reapply them back to the button.
- (void)enableBackgroundImageStrechingWithLeftCapWidth:(float)leftCapWidth withTopCapHeight:(float)topCapHeight;
{
UIImage *backgroundimageNormal = [self backgroundImageForState:UIControlStateNormal];
if (backgroundimageNormal != nil)
{
UIImage *stretchImage = [backgroundimageNormal stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateNormal];
}
UIImage *backgroundimageSelected = [self backgroundImageForState:UIControlStateSelected];
if (backgroundimageSelected != nil)
{
UIImage *stretchImage = [backgroundimageSelected stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateSelected];
}
UIImage *backgroundimageHighlighted = [self backgroundImageForState:UIControlStateHighlighted];
if (backgroundimageHighlighted != nil)
{
UIImage *stretchImage = [backgroundimageHighlighted stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateHighlighted];
}
UIImage *backgroundimageDisabled = [self backgroundImageForState:UIControlStateDisabled];
if (backgroundimageDisabled != nil)
{
UIImage *stretchImage = [backgroundimageDisabled stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[self setBackgroundImage:stretchImage forState:UIControlStateDisabled];
}
}
Seems to work except the button is now not clickable
It seems that the highlighted state causes the issue!
I have removed the highlighted block and it works fine?
If you do not have custom images for highlighted/disabled states, but rely on UIButton to apply the highlight/disabled effect, then [self backgroundImageForState: UIControlStateHighlighted] will NOT return nil. It will instead return a pointer to the normal state image.
Using your code, you are then effectively setting identical images for all states. They look like custom images to the framework, though. This disables the built-in highlight/disabled effects.