简体   繁体   中英

How To reset the original background image of button in Objective-C?

Im currently working on an app made by someone else. I want to change some things to see how it will work. Currently you have one option to choose an answer and cant change it to something different. I want to change that. I want the user to be able to change the answer before submitting it. The problem is I dont know the name or where i can find the original image for the button. Is there a way to just reset to its original? Currently when you drag and drop the answer button on correct answer button(I know its confusing. in this app you drag a button with an image on answer button and the image changes). When i try to change the answer the image changes also but previous selections also stays selected.

(Sorry dont know how to properly post code in here)

- (void) setDefaultImage
{

    // 1) set all the answer button images to unselcted image
    [Ans11 setBackgroundImage:[UIImage imageNamed:@""]         forState:UIControlStateNormal];
    [Ans12 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans13 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans14 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans21 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans22 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans23 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans24 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans31 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans32 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans33 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [Ans34 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

}
- (void) buttonDroppedOnAnAnswer:(UIButton *)droppedButton
{
    if(CGRectIntersectsRect(droppedButton.frame, Ans11.frame))
    {
        [Ans11 setBackgroundImage:[UIImage imageNamed:@"ans2.png"]    forState:UIControlStateNormal];
        //[Ans11 setImage:nil];
    }
    else if(CGRectIntersectsRect(droppedButton.frame, Ans12.frame))
    {
        [Ans12 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
        //[Ans12 setImage:nil];
    }
    else if(CGRectIntersectsRect(droppedButton.frame, Ans13.frame))
    {
        [Ans13 setBackgroundImage:[UIImage imageNamed:@"ans2.png"]    forState:UIControlStateNormal];
        //[Ans13 setImage:nil];
    }
    else if(CGRectIntersectsRect(droppedButton.frame, Ans14.frame))
    {
        [Ans14 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
        //[Ans14 setImage:nil];
    }
    [droppedButton setUserInteractionEnabled:YES];
}
//Method working when the end of touch is recognized:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self setDefaultImage];
    //Assigning the coordinations of the initial position of but1, but2,   but3, but4:
CGPoint position1;
position1.y=226;
position1.x=974;

CGPoint position2;
position2.y=301;
position2.x=974;

CGPoint position3;
position3.y=377;
position3.x=974;

CGPoint position4;
position4.y=458;
position4.x=974;

self.view.multipleTouchEnabled = NO;

 //Long list of conditions, what will happen if each but will intersect with each picture or each answer:
//if but1 will be dropped on Ans11...
if (CGRectIntersectsRect(but1.frame, Ans11.frame))
{
    [self buttonDroppedOnAnAnswer:but1];
    //...the backgound of Ans11 will change for ans2.png image:
    [Ans11 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    //User interaction is enabled to allow reuse of the button:
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans12.frame))
{
    [self buttonDroppedOnAnAnswer:but1];
    [Ans12 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    //[Ans11 setImage:nil];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans13.frame))
{
    [self buttonDroppedOnAnAnswer:but1];
    [Ans13 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans14.frame))
{
    [self buttonDroppedOnAnAnswer:but1];
    [Ans14 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans21.frame))
{
    [Ans21 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans22.frame))
{
    [Ans22 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}
else if (CGRectIntersectsRect(but1.frame, Ans23.frame))
{
    [Ans23 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    but1.userInteractionEnabled=YES;
}

EDIT: I have figured out a different way to do this. Instead i added another draggable button which simply cancel the previous selection. This is a much simpler way but might require more repetitive lines of code. Thank you very much for your help!!

Suppose the below method is called when you drag and drop the button to any answer -

- (void) buttonDroppedOnAnAnswer
{
   // 1) set all the answer button images to unselcted image
   // Suppose we have 4 answer buttons then set
    [Ans11 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans12 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans13 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans14 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];

// check the intersection of button on the answer
// set the intersected answer button image to selected image

   if(CGRectIntersectsRect(but1.frame, Ans11.frame))
   {
      [Ans11 setBackgroundImage:[UIImage imageNamed:@"ans1.png"] forState:UIControlStateNormal];
    //[Ans11 setImage:nil];
      but1.userInteractionEnabled=YES;
   }
   else if(CGRectIntersectsRect(but1.frame, Ans12.frame))
   {
      [Ans12 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    //[Ans12 setImage:nil];
      but1.userInteractionEnabled=YES;
   }
   else if(CGRectIntersectsRect(but1.frame, Ans13.frame))
   {
      [Ans13 setBackgroundImage:[UIImage imageNamed:@"ans3.png"] forState:UIControlStateNormal];
      //[Ans13 setImage:nil];
      but1.userInteractionEnabled=YES;
   }
   else if(CGRectIntersectsRect(but1.frame, Ans14.frame))
   {
      [Ans14 setBackgroundImage:[UIImage imageNamed:@"ans4.png"] forState:UIControlStateNormal];
    //[Ans14 setImage:nil];
      but1.userInteractionEnabled=YES;
   }
}

In case if you want to select 4 answers with 4 different buttons then you should first set all the answer buttons to default images (during initialization).

- (void) setDeafultImagesOnAnswerButtons
{
// 1) set all the answer button images to unselcted image
   // Suppose we have 4 answer buttons then set
    [Ans11 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans12 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans13 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
    [Ans14 setBackgroundImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
}

and then just check the intersection of the dropped button on the answer and set the corresponding image, as depicted in below function -

// if you have more than one drop button call the function with the corresponding dropped button

- (void) buttonDroppedOnAnAnswer:(UIButton *)droppedButton
{
// check the intersection of button on the answer
// set the intersected answer button image to selected image

   if(CGRectIntersectsRect(droppedButton.frame, Ans11.frame))
   {
      [Ans11 setBackgroundImage:[UIImage imageNamed:@"ans1.png"] forState:UIControlStateNormal];
    //[Ans11 setImage:nil];
   }
   else if(CGRectIntersectsRect(droppedButton.frame, Ans12.frame))
   {
      [Ans12 setBackgroundImage:[UIImage imageNamed:@"ans2.png"] forState:UIControlStateNormal];
    //[Ans12 setImage:nil];
   }
   else if(CGRectIntersectsRect(droppedButton.frame, Ans13.frame))
   {
      [Ans13 setBackgroundImage:[UIImage imageNamed:@"ans3.png"] forState:UIControlStateNormal];
      //[Ans13 setImage:nil];
   }
   else if(CGRectIntersectsRect(droppedButton.frame, Ans14.frame))
   {
      [Ans14 setBackgroundImage:[UIImage imageNamed:@"ans4.png"] forState:UIControlStateNormal];
    //[Ans14 setImage:nil];
   }
      [droppedButton setUserInteractionEnabled:YES];
}

The backgroundImageForState method of UIButton returns the UIImage

- (UIImage *)backgroundImageForState:(UIControlState)state

So pretty much how you described:

UIImage *defaultImage = [UIImage imageNamed:@"default.png"];

// Suppose you have initially set the image of the button as -

UIButton * answerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[answerButton setImage:defaultImage forState:UIControlStateNormal];

// Later to Check

if ([answerButton backgroundImageForState:UIControlStateNormal] == defaultImage) {
    // as the answer button background already contains the default image
    // we need not to set the image
}
else
{
    // as the answer button background contains some other image than the  default image so we need to set the default image to button background
UIImage *defaultImage = [UIImage imageNamed:@"default.png"];
[answerButton setImage:defaultImage forState:UIControlStateNormal];
}

As per your requirements, I am posting the code -

Let me summarize the requirements first - (please update me if I am wrong somewhere)

There is a view where you need to show a question to a user which will have multiple choices and user can select multiple options from those choices.

The choices are shown up with the buttons, also there are some buttons which are used to select the choices.

The buttons which are used to select the answer buttons are dragged and dropped on the respective answer buttons.

Initially all the answer buttons will be set to default images, and the also the drag-able buttons will be set to their respective default images.

When any drag-able button is dropped on any answer button than the answer button should be set to some selected image, if any other drag-able button is dropped on some other answer button than the corresponding answer button will also be set to some selected image and so on all the answer buttons will be set to selected image as the buttons are dropped on them.

Also if user taps on a selected answer button image than the image of that answer button will be set to default image.

For instance, I consider there are - 1) 4 drag-able buttons 2) 12 answer buttons

The 4 drag-able buttons will be set to there respective default images, lets say dragButImage1.png, dragButImage2.png, dragButImage3.png, dragButImage4.png. Also we will add the tag value 1, 2, 3, 4 to these buttons.

Also, the 12 answer buttons will be set to there respective default images, lets say ans1.png, ans2.png, ans3.png, ans4.png, ans5.png, ans6.png, ans7.png, ans8.png, ans9.png, ans10.png, ans11.png, ans12.png. And the tag value for these buttons will be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12.

Now the coding part

- (void) initializeView
{
// initialize drag-able buttons to default images
    [button1 setTag:1];
    [button1 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"dragButImage%d.png", button1.tag]] forState:UIControlStateNormal];

[button2 setTag:2];
[button2 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"dragButImage%d.png", button2.tag]] forState:UIControlStateNormal];

[button3 setTag:3];
[button3 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"dragButImage%d.png", button3.tag]] forState:UIControlStateNormal];

[button4 setTag:4];
[button4 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"dragButImage%d.png", button4.tag]] forState:UIControlStateNormal];

// initialize anser buttons to default images

[ans1 setTag:1];
[ans1 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans1.tag]] forState:UIControlStateNormal];
[ans1 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans2 setTag:2];
[ans2 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans2.tag]] forState:UIControlStateNormal];
[ans2 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans3 setTag:3];
[ans3 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans3.tag]] forState:UIControlStateNormal];
[ans3 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans4 setTag:4];
[ans4 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans4.tag]] forState:UIControlStateNormal];
[ans4 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans5 setTag:5];
[ans5 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans5.tag]] forState:UIControlStateNormal];
[ans5 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans6 setTag:6];
[ans6 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans6.tag]] forState:UIControlStateNormal];
[ans6 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans7 setTag:7];
[ans7 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans7.tag]] forState:UIControlStateNormal];
[ans7 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans8 setTag:8];
[ans8 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans8.tag]] forState:UIControlStateNormal];
[ans8 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans9 setTag:9];
[ans9 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans9.tag]] forState:UIControlStateNormal];
[ans9 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans10 setTag:10];
[ans10 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans10.tag]] forState:UIControlStateNormal];
[ans10 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans11 setTag:11];
[ans11 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans11.tag]]forState:UIControlStateNormal];
[ans11 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];

[ans12 setTag:12];
[ans12 setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", ans12.tag]] forState:UIControlStateNormal];
[ans12 addTarget:self action:@selector(setMyDefaultImage:) forControlEvents:UIControlEventTouchUpInside];
}

// the below function will be invoked when a drag-able button is dropped on an answer

- (void) aButton:(UIButton *)droppedButton droppedOnAnswer:(UIButton *)answerButton
{
// set the answer button image to selected image
          [answerButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ansSelectedImage%d.png", answerButton.tag]] forState:UIControlStateNormal];

// you could also set the drag-able button image to selected image same as above
[droppedButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"droppedButtonSelectedImage%d.png", droppedButton.tag]] forState:UIControlStateNormal];
}

// called when any answer button is touched // this method will set the default image of the touched answer button

-(void) setMyDefaultImage:(UIButton*)answerButton
{
    [answerButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ans%d.png", answerButton.tag]] forState:UIControlStateNormal];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM