Why UIButton requires two clicks to change its image











up vote
0
down vote

favorite












I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:



 @IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}


However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.



My question is why the image was not changed in button2 after the first click?



What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?










share|improve this question
























  • i think it's simulator bug.your code is correct(please change sender from Any to UIButton)
    – andesta.erfan
    Nov 17 at 15:04












  • That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.
    – rptwsthi
    Nov 17 at 15:10










  • @J.Doe i said i think. if you know the correct answer please post it because it's as you say basic
    – andesta.erfan
    Nov 17 at 16:05












  • Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.
    – Mozahler
    Nov 17 at 16:13










  • @J.Doe so please ask and try to answer and do not interrupt the community
    – andesta.erfan
    Nov 17 at 16:36















up vote
0
down vote

favorite












I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:



 @IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}


However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.



My question is why the image was not changed in button2 after the first click?



What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?










share|improve this question
























  • i think it's simulator bug.your code is correct(please change sender from Any to UIButton)
    – andesta.erfan
    Nov 17 at 15:04












  • That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.
    – rptwsthi
    Nov 17 at 15:10










  • @J.Doe i said i think. if you know the correct answer please post it because it's as you say basic
    – andesta.erfan
    Nov 17 at 16:05












  • Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.
    – Mozahler
    Nov 17 at 16:13










  • @J.Doe so please ask and try to answer and do not interrupt the community
    – andesta.erfan
    Nov 17 at 16:36













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:



 @IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}


However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.



My question is why the image was not changed in button2 after the first click?



What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?










share|improve this question















I am new to iOS app development using Swift 4. I used the code below to change the image of button2 by running it in the iOS simulator:



 @IBAction func button2(_ sender: Any) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}


However, button2 was highlighted when I first click on it without changing its image. Then after the second click, the image has been changed in button2.



My question is why the image was not changed in button2 after the first click?



What can I do to change the image after the first click instead of twice? Is this a bug in the iOS simulator of Xcode or it is normal?







ios swift uibutton






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 16:25









rmaddy

235k27305371




235k27305371










asked Nov 17 at 14:29









Hosy Hosy

63




63












  • i think it's simulator bug.your code is correct(please change sender from Any to UIButton)
    – andesta.erfan
    Nov 17 at 15:04












  • That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.
    – rptwsthi
    Nov 17 at 15:10










  • @J.Doe i said i think. if you know the correct answer please post it because it's as you say basic
    – andesta.erfan
    Nov 17 at 16:05












  • Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.
    – Mozahler
    Nov 17 at 16:13










  • @J.Doe so please ask and try to answer and do not interrupt the community
    – andesta.erfan
    Nov 17 at 16:36


















  • i think it's simulator bug.your code is correct(please change sender from Any to UIButton)
    – andesta.erfan
    Nov 17 at 15:04












  • That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.
    – rptwsthi
    Nov 17 at 15:10










  • @J.Doe i said i think. if you know the correct answer please post it because it's as you say basic
    – andesta.erfan
    Nov 17 at 16:05












  • Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.
    – Mozahler
    Nov 17 at 16:13










  • @J.Doe so please ask and try to answer and do not interrupt the community
    – andesta.erfan
    Nov 17 at 16:36
















i think it's simulator bug.your code is correct(please change sender from Any to UIButton)
– andesta.erfan
Nov 17 at 15:04






i think it's simulator bug.your code is correct(please change sender from Any to UIButton)
– andesta.erfan
Nov 17 at 15:04














That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.
– rptwsthi
Nov 17 at 15:10




That may because you have set image to normal state and the button must be in highlighted state. But do check that code on device.
– rptwsthi
Nov 17 at 15:10












@J.Doe i said i think. if you know the correct answer please post it because it's as you say basic
– andesta.erfan
Nov 17 at 16:05






@J.Doe i said i think. if you know the correct answer please post it because it's as you say basic
– andesta.erfan
Nov 17 at 16:05














Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.
– Mozahler
Nov 17 at 16:13




Notice that you call the button "sender" in the method. You need to update sender, not button2. This would be clearer if you updated sender to UIButton (which is what it is) instead of using Any. See my answer below.
– Mozahler
Nov 17 at 16:13












@J.Doe so please ask and try to answer and do not interrupt the community
– andesta.erfan
Nov 17 at 16:36




@J.Doe so please ask and try to answer and do not interrupt the community
– andesta.erfan
Nov 17 at 16:36












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You probably have an issue related to UIButton states that is causing this problem.
I don't think it is a simulator bug.
By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



@IBAction func buttonTapped(_ sender: Any) {
button.setImage(UIImage(named: "image"), for: .normal)
}


Try this:



override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(named: "image"), for: .selected)
}

@IBAction func buttonTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}


And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.






share|improve this answer























  • Thank Matias, it is an issue related to UIButton states that is causing this problem. and it is not a simulator bug
    – Hosy Hosy
    Nov 17 at 20:47










  • @HosyHosy it worked for you or you still need help?
    – Matias Jurfest
    2 days ago


















up vote
0
down vote













Rename your method/action so it differs from the button/property.



Change Any to UIButton since you know its class.



@IBAction func buttonTapped(_ buttonTapped: UIButton) {
buttonTapped. button.isSelected = !button.isSelected
}


Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.






share|improve this answer























  • Thank Mozahler but it didn't work
    – Hosy Hosy
    Nov 17 at 20:45










  • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?
    – Mozahler
    Nov 17 at 22:56












  • No, I used the following code and it works: button2.setImage(UIImage(named: "wrong_answer"), for: .selected); button2.isSelected = true;
    – Hosy Hosy
    2 days ago












  • Excellent. Glad you figured it out!
    – Mozahler
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352159%2fwhy-uibutton-requires-two-clicks-to-change-its-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You probably have an issue related to UIButton states that is causing this problem.
I don't think it is a simulator bug.
By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



@IBAction func buttonTapped(_ sender: Any) {
button.setImage(UIImage(named: "image"), for: .normal)
}


Try this:



override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(named: "image"), for: .selected)
}

@IBAction func buttonTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}


And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.






share|improve this answer























  • Thank Matias, it is an issue related to UIButton states that is causing this problem. and it is not a simulator bug
    – Hosy Hosy
    Nov 17 at 20:47










  • @HosyHosy it worked for you or you still need help?
    – Matias Jurfest
    2 days ago















up vote
1
down vote



accepted










You probably have an issue related to UIButton states that is causing this problem.
I don't think it is a simulator bug.
By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



@IBAction func buttonTapped(_ sender: Any) {
button.setImage(UIImage(named: "image"), for: .normal)
}


Try this:



override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(named: "image"), for: .selected)
}

@IBAction func buttonTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}


And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.






share|improve this answer























  • Thank Matias, it is an issue related to UIButton states that is causing this problem. and it is not a simulator bug
    – Hosy Hosy
    Nov 17 at 20:47










  • @HosyHosy it worked for you or you still need help?
    – Matias Jurfest
    2 days ago













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You probably have an issue related to UIButton states that is causing this problem.
I don't think it is a simulator bug.
By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



@IBAction func buttonTapped(_ sender: Any) {
button.setImage(UIImage(named: "image"), for: .normal)
}


Try this:



override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(named: "image"), for: .selected)
}

@IBAction func buttonTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}


And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.






share|improve this answer














You probably have an issue related to UIButton states that is causing this problem.
I don't think it is a simulator bug.
By the way, a good practice you should follow is to name the outlet different than the @IBAction. Let's say:



@IBAction func buttonTapped(_ sender: Any) {
button.setImage(UIImage(named: "image"), for: .normal)
}


Try this:



override func viewDidLoad() {
super.viewDidLoad()
button.setImage(UIImage(named: "image"), for: .selected)
}

@IBAction func buttonTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}


And then the image will be updated automatically when you tap on the button. You can change it to button.isSelected = true if you want to keep the image after the first tap.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 at 15:54

























answered Nov 17 at 15:20









Matias Jurfest

402412




402412












  • Thank Matias, it is an issue related to UIButton states that is causing this problem. and it is not a simulator bug
    – Hosy Hosy
    Nov 17 at 20:47










  • @HosyHosy it worked for you or you still need help?
    – Matias Jurfest
    2 days ago


















  • Thank Matias, it is an issue related to UIButton states that is causing this problem. and it is not a simulator bug
    – Hosy Hosy
    Nov 17 at 20:47










  • @HosyHosy it worked for you or you still need help?
    – Matias Jurfest
    2 days ago
















Thank Matias, it is an issue related to UIButton states that is causing this problem. and it is not a simulator bug
– Hosy Hosy
Nov 17 at 20:47




Thank Matias, it is an issue related to UIButton states that is causing this problem. and it is not a simulator bug
– Hosy Hosy
Nov 17 at 20:47












@HosyHosy it worked for you or you still need help?
– Matias Jurfest
2 days ago




@HosyHosy it worked for you or you still need help?
– Matias Jurfest
2 days ago












up vote
0
down vote













Rename your method/action so it differs from the button/property.



Change Any to UIButton since you know its class.



@IBAction func buttonTapped(_ buttonTapped: UIButton) {
buttonTapped. button.isSelected = !button.isSelected
}


Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.






share|improve this answer























  • Thank Mozahler but it didn't work
    – Hosy Hosy
    Nov 17 at 20:45










  • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?
    – Mozahler
    Nov 17 at 22:56












  • No, I used the following code and it works: button2.setImage(UIImage(named: "wrong_answer"), for: .selected); button2.isSelected = true;
    – Hosy Hosy
    2 days ago












  • Excellent. Glad you figured it out!
    – Mozahler
    2 days ago















up vote
0
down vote













Rename your method/action so it differs from the button/property.



Change Any to UIButton since you know its class.



@IBAction func buttonTapped(_ buttonTapped: UIButton) {
buttonTapped. button.isSelected = !button.isSelected
}


Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.






share|improve this answer























  • Thank Mozahler but it didn't work
    – Hosy Hosy
    Nov 17 at 20:45










  • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?
    – Mozahler
    Nov 17 at 22:56












  • No, I used the following code and it works: button2.setImage(UIImage(named: "wrong_answer"), for: .selected); button2.isSelected = true;
    – Hosy Hosy
    2 days ago












  • Excellent. Glad you figured it out!
    – Mozahler
    2 days ago













up vote
0
down vote










up vote
0
down vote









Rename your method/action so it differs from the button/property.



Change Any to UIButton since you know its class.



@IBAction func buttonTapped(_ buttonTapped: UIButton) {
buttonTapped. button.isSelected = !button.isSelected
}


Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.






share|improve this answer














Rename your method/action so it differs from the button/property.



Change Any to UIButton since you know its class.



@IBAction func buttonTapped(_ buttonTapped: UIButton) {
buttonTapped. button.isSelected = !button.isSelected
}


Make sure that you are receiving the button callbacks by declaring your view controller a UIButtonDelegate and set the button's delegate property to self.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 at 23:02

























answered Nov 17 at 16:08









Mozahler

2,02041625




2,02041625












  • Thank Mozahler but it didn't work
    – Hosy Hosy
    Nov 17 at 20:45










  • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?
    – Mozahler
    Nov 17 at 22:56












  • No, I used the following code and it works: button2.setImage(UIImage(named: "wrong_answer"), for: .selected); button2.isSelected = true;
    – Hosy Hosy
    2 days ago












  • Excellent. Glad you figured it out!
    – Mozahler
    2 days ago


















  • Thank Mozahler but it didn't work
    – Hosy Hosy
    Nov 17 at 20:45










  • Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?
    – Mozahler
    Nov 17 at 22:56












  • No, I used the following code and it works: button2.setImage(UIImage(named: "wrong_answer"), for: .selected); button2.isSelected = true;
    – Hosy Hosy
    2 days ago












  • Excellent. Glad you figured it out!
    – Mozahler
    2 days ago
















Thank Mozahler but it didn't work
– Hosy Hosy
Nov 17 at 20:45




Thank Mozahler but it didn't work
– Hosy Hosy
Nov 17 at 20:45












Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?
– Mozahler
Nov 17 at 22:56






Did you declare your view controller a UIButtonDelegate and set the button's delegate property to self?
– Mozahler
Nov 17 at 22:56














No, I used the following code and it works: button2.setImage(UIImage(named: "wrong_answer"), for: .selected); button2.isSelected = true;
– Hosy Hosy
2 days ago






No, I used the following code and it works: button2.setImage(UIImage(named: "wrong_answer"), for: .selected); button2.isSelected = true;
– Hosy Hosy
2 days ago














Excellent. Glad you figured it out!
– Mozahler
2 days ago




Excellent. Glad you figured it out!
– Mozahler
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352159%2fwhy-uibutton-requires-two-clicks-to-change-its-image%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin