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)
Related
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)
}
I want to let user give +1 with one button or -1 points with another button, but they should be able to only press one of these buttons one time...
I use this code, but the user can still click on the button multiple times...
var job: Job! {
didSet {
jobLabel.text = job.text
likeButton.setTitle("👍 \(job.numberOfLikes)", for: [])
dislikeButton.setTitle("👎 \(job.numberOfDislikes)", for: [])
}
}
#IBAction func dislikeDidTouch(_ sender: AnyObject)
{
(dislikeDidTouch).isEnabled = false
job.dislike()
dislikeButton.setTitle("👎 \(job.numberOfDislikes)", for: [])
dislikeButton.setTitleColor(dislikeColor, for: []) }
#IBAction func likeDidTouch(_ sender: AnyObject)
{
sender.userInteractionEnabled=YES;
job.like()
likeButton.setTitle("👍 \(job.numberOfLikes)", for: [])
likeButton.setTitleColor(likeColor, for: [])
}
Since the sender is a UIButton , it's better to construct the funcs like this
#IBAction func dislikeDidTouch(_ sender: UIButton)
#IBAction func likeDidTouch(_ sender: UIButton)
and inside each one do
sender.isEnabled = false
How to identify in swift if which button was pressed i have 3 button actions.now i have another button is which when pressed it will identify which button from the 3 was pressed. cause first is you have to click from the 3 button after clicking from the 3 button then you will click
idenfifywhichpressed button and this button will identify or print which button from the 3 was pressed.
//this button will identify
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
}
the 3 buttons
#IBAction func btn1(_ sender: UIButton) {
}
#IBAction func btn2(_ sender: UIButton) {
}
#IBAction func btn3(_ sender: UIButton) {
}
Try something like this
Declare an enum
enum SelectedButtonTag: Int {
case First
case Second
case Third
}
Button Action
Connect the three button actions to this method with different tag
#IBAction func idenfifywhichpressed(sender: UIButton) {
switch sender.tag {
case SelectedButtonTag.First.rawValue:
print("do something when first button is tapped")
case SelectedButtonTag.Second.rawValue:
print("do something when second button is tapped")
case SelectedButtonTag.Third.rawValue:
print("do something when third button is tapped")
default:
print("default")
}
}
Small improvement on #prajnaranjan-das answer: casting to an enum immediately to clean up some cruft and remove the need to implement default in the switch...
enum ButtonTag: Int {
case First
case Second
case Third
}
func buttonTapped(_ sender: UIButton) {
guard let knownSender = ButtonTag(rawValue: sender.tag) else { return }
switch knownSender {
case .First:
print("do something when first button is tapped")
case .Second:
print("do something when second button is tapped")
case .Third:
print("do something when third button is tapped")
}
}
This is helped for me (swift 5.0)
#IBAction func hardnessSelected(_ sender: UIButton) {
print(sender.currentTitle) // <----------
}
}
Do something like this,
var index:Int = 0
Now do this, FYI, set button tags accordingly 1,2 and 3...
#IBAction func btn1(_ sender: UIButton) {
index = sender.tag
}
#IBAction func btn2(_ sender: UIButton) {
index = sender.tag
}
#IBAction func btn3(_ sender: UIButton) {
index = sender.tag
}
now this,
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
if index == 1 {
//Button 1 Pressed
}else if index == 2 {
//Button 2 Pressed
} else if index == 3{
//Button 3 Pressed
}
}
FYI. You can use Switch statement as well in idenfifywhichpressed method.
Update. Do not create three methods for btn1, btn2 and btn3, just create one method and assign tag accordingly for simplicity.
I think you can take a variable like
var checkBtn: String?
inside 1st button change the variable value
checkBtn = "1"
inside 2nd button change the variable value
checkBtn = "2"
inside 3rd button change the variable value
checkBtn = "3"
#IBAction func idenfifywhichpressed(_ sender: UIButton) {
// just print the value of CheckBtn
print(checkBtn)
//this will give you which btn pressed right
}
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 tried to get the title of a button in swift like below.
#IBAction func buttonAction(_ sender: Any) {
let buttonTitle = sender.titleForState(.Normal)!
}
but it didn't work,even it doesn't give any hint when we press . after the sender.
so what is the correct way of doing this in swift 3.0
Or else if we create an IBOutlet and then we use its currentTitle, it works fine like below. Why we cannot get it with sender. for above
#IBOutlet var thebutton: UIButton!
#IBAction func buttonAction(_ sender: Any) {
let buttonTitle = thebutton.currentTitle!
print(buttonTitle)
}
Because parameter sender is in type Any instead of UIButton. Change the method signature to:
#IBAction func buttonAction(_ sender: UIButton) {
if let buttonTitle = sender.title(for: .normal) {
print(buttonTitle)
}
}
and you should be good to go.
Smartest version using Swift 5.0:
#IBAction func buttonAction(_ sender: UIButton) {
print(sender.currentTitle)
}
To get the title of the button regardless of its current state in swift 3.0 try using this:
#IBAction func buttonPressed(_ sender:UIButton){
let buttonTitle = sender.titleLabel?.text
print("\(String(describing: buttonTitle)")
}
This will return the title for the state, based on the state that the button is current in.
In order to resolve the warning, following code may be used.
#IBAction func buttonAction(_ sender: UIButton) {
let buttonTitle = sender.title(for: .normal) ?? String()
print(buttonTitle)
}
Using Swift version 5.5.2, the best way I found out to print a label's title is:
#IBAction func keyPressed(_ sender: UIButton) {
print(sender.title(for: .normal)!)
}
I was receiving a warning while using sender.title(for: .normal). The exclamation mark (!) solved it.
#IBAction func buttonPressed(_ sender: AnyObject) {
let title = sender.title(for: .normal)
print("\(title) button pressed")
}
So this is for swift 4.0, not the last one.
If your button has a regular title you can use:
button.title(for: state) // probably UIControl.State.normal
If your UIButton has an attributed title you'll need to use:
self.attributedTitle(for: state)
Or you can use a more general extension:
extension UIButton {
func titleForState(_ state: UIControl.State = .normal) -> String? {
if let title = self.title(for: state) {
return title
}
if let attribTitle = self.attributedTitle(for: state) {
return attribTitle.string
}
return nil
}
}
I used switch statment:
var buttonConter = 1
#IBAction func hardlessSelected(_ sender: UIButton) {
switch buttonConter {
case 1:
buttonConter += 1
print("Soft")
case 2:
buttonConter += 1
print("Medium")
case 3:
buttonConter += 1
print("Hard")
default:
print("Non")
}
}
}
The simple way just to use currentTitle:
#IBAction func hardlessSelected(_ sender: UIButton) {
print(sender.currentTitle)
}
Instead of using the currentTitle of the UIButton to get the name, you can use titleLabel.
The easy way to get this done is to unwrap the titleLabel then unwrap the text:
#IBAction func buttonAction(_ sender: UIButton) {
var buttonTitle = sender.titleLabel!.text!
print(buttonTitle)
}
Since Swift 3.0, it changed from
sender.titleForState(.Normal)! >> sender.title(for : .normal)!
Change Any to AnyObject.
#IBAction func buttonPressed(_ sender: AnyObject) {
let title = sender.title(for: .normal)!
print("\(title) button pressed")
}
#IBAction func btnNextTapped(_ sender: Any) {
let title = (sender as AnyObject).title(for: .normal)!
}
Swift4.0