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?
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.
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.
it's simulator bug. it worked on a real device
#IBAction func button2(_ sender: UIButton) {
button2.setImage(UIImage(named: "wrong_answer"), for: .normal)
}
Related
I am trying to add button programmatically, I created button, it shows in app, but problem that when I write .addTarget in #selector occurs error (it builds successfully, but action do not work) I tried to write playAction() and playAction(sender:) and playAction. None of them work.
playPauseButton.addTarget(self, action: #selector(playAction(sender:)), for: .touchUpInside)
#objc
func playAction(sender: UIButton){
print("Hello")
}
UPD: I solved by creating just system button and changed it. Maybe my Xcode have bug and because of that occurs error.
Change with
playPauseButton.addTarget(self, action: #selector(playAction), for: .touchUpInside)
this is ok:
#objc
func playAction(sender: UIButton) {
print("Hello")
}
works for me
Your function needs to have the #objc attribute. This allows it to be looked-up as a selector.
#objc
func playAction(sender: UIButton) {
print("Hello")
}
As homework, I need to make simple calculator to calculate installments. I can't find and method to connect a UISlider and a UITextField to synchronize both to choose correct data.
I tried it, but it works for slider to textfield :
#IBAction func change(_ sender: Any) {
TextField.text = String(Int(slider.value))
}
try use this, IBAction can be irritating at the beginning of learning
override viedDidLoad() {
super.viewDidLoad()
addTarget()
}
func addTarget() {
slider.addTarget(self, action: #selector(onSliderChange), for: UIControl.Event.valueChanged)
}
//callback called when value change
#objc func onSliderChange(){
textField.text = String(Int(slider.value))
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
1I am facing a problem in iOS swift. I am trying to hide and show the UIButton text that is added in the UITextview list. I want to hide the button text on click and show on re-click the button. I can't keep nil the button text because i give the background colour to the button according to the alphabet.
[]
Please help me
Thanks in advance
My problem is not solved
Setting your button title color in state normal and selected state
When click to button, just simple change the state.
// Settings
// let button = <your button>
button.setTitleColor(<yourColor>, for: .normal)
button.setTitleColor(UIColor.clear, for: .selected)
// Action
#IBAction func didSelectButton(_ button: UIButton) {
button.isSelected = !button.isSelected
}
As you are saying that you can't nil the button's text, you should do this,
You can implement this Bool extension also,
extension Bool {
mutating func toggle() {
self = !self
}
}
#IBAction func myButton(_ sender: UIButton) {
sender.titleLabel?.isHidden.toggle()
}
this will show and hide your Button's titleLabel text.
UPDATE
#IBAction func btnTapped(_ sender: UIButton) {
sender.isSelected.toggle()
if sender.isSelected == true {
sender.setTitleColor(UIColor.clear, for: .normal)
} else {
sender.setTitleColor(UIColor.blue, for: .normal)
}
}
#IBAction func myButton(_ sender: UIButton) {
if sender.currentTitle = "" {
sender.setTitle("myTitle", for: .normal)
} else {
sender.setTitle("", for: .normal)
}
}
override func viewDidLoad() {
super.viewDidLoad()
myButton.setTitle("myTitle", for: .normal)
myButton.setTitle("", for: .selected)
}
#IBAction func myButtonClicked(_ sender: UIButton) {
myButton.isSelected = !myButton.isSelected
}
I have code to copy the numbers from the Label.
How it's called?
How can I make that when I click on a button, it appears that on the photo
#IBAction func copybutton(_ sender: UIButton) {
UIPasteboard.general.string = displayResultLabel.text
}
Use this code -
UIPasteboard.general.string = sender.title(for: UIControlState.normal)
I’m trying to change the image of a button when tapped (it’s for an audio player that I want Play to change to Pause). So far I’ve only managed to change it when the button is held, then it changes back when released.
I’ve tried using Selected instead of Highlighted but that doesn’t work. (The initial image is set in the Attributes Inspector). This is what I have -
#IBAction func buttonTapped(sender: AnyObject) {
ColourTestButton.setImage(UIImage(named: "blank-purple.jpg"), forState:.Highlighted)
}
I realise Highlighted means just that though on another image where I wanted it to change back (in a Cell of a Table View) it stayed, which is why I thought it would work for this one. What do I have to do here to make it stay changed to the new image?
I think you need this code:
#IBAction func buttonTapped(sender: AnyObject) {
let image = UIImage(named: "blank-purple.jpg") as UIImage!
let playButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
ColourTestButton.setImage(image, forState: .Normal)
}
UPDATE:
If you want to change the image like play/pause then you can do it this way:
var pressed = false
#IBAction func pressed(sender: AnyObject) {
if !pressed {
let image = UIImage(named: "pauseImage.png") as UIImage!
let playButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
btn.setImage(image, forState: .Normal)
pressed = true
} else {
let image = UIImage(named: "playImage.png") as UIImage!
let playButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
btn.setImage(image, forState: .Normal)
pressed = false
}
}
I think you are updating the image only at Highlighted state, so when you release the button it goes back to an original image. Try to change it at Normal state so that it will change the image. Take one flag to check the Play and Pause condition.
#IBAction func buttonTapped(sender: AnyObject) {
if (!flag)
{
//for Play
flag = true
ColourTestButton.setImage(UIImage(named:"USE PLAY IMAGE"), forState:. Normal)
}
else
{
//for Pause
flag = false
ColourTestButton.setImage(UIImage(named:"USE PAUSE IMAGE"), forState:. Normal)
}
}
Though Dharmesh Kheni's answer is OK I would recommend to use two buttons. You can set them in separate function or in interface builder and then just show and hide them according to state. Something like this:
func setButtons()
{
playBtn.setImage(UIImage(named: "play.png"), forState:.Normal)
pauseBtn.setImage(UIImage(named: "play.png"), forState:.Normal)
pauseBtn.hidden = true
}
#IBAction func playTapped(sender: AnyObject)
{
playBtn.hidden = true
pauseBtn.hidden = flase
//do other stuff
}
#IBAction func pauseTapped(sender: AnyObject)
{
playBtn.hidden = false
pauseBtn.hidden = true
//do other stuff
}