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?
ios swift uibutton
|
show 1 more comment
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?
ios swift uibutton
i think it's simulator bug.your code is correct(please change sender fromAny
toUIButton
)
– 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
|
show 1 more comment
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?
ios swift uibutton
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
ios swift uibutton
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 fromAny
toUIButton
)
– 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
|
show 1 more comment
i think it's simulator bug.your code is correct(please change sender fromAny
toUIButton
)
– 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
|
show 1 more comment
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.
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
add a comment |
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.
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 toself
?
– 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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
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 toself
?
– 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
add a comment |
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.
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 toself
?
– 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
add a comment |
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.
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.
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 toself
?
– 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
add a comment |
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 toself
?
– 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
i think it's simulator bug.your code is correct(please change sender from
Any
toUIButton
)– 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