Swap the label text of two buttons in Swift - ios

I have 2 button like picture below, their text label are: "Input location...". Now i want to click on the swap button on the right, so the text label of these button will swap. It's look like you have 2 location and you want to swap it. Please any one can point me how to do it. Thank you very much.

On click just set their text to each other.
var tempString = secondButton.titleLabel.text
secondButton.setTitle(firstButton.titleLabel.text, forState: UIControlState.Normal)
firstButton.setTitle(tempString, forState: UIControlState.Normal)

#IBOutlet var firstButton: UIButton!
#IBOutlet var secondButton: UIButton!
#IBAction func didTouchUpInsideSwapButton() {
let firstButtonText = firstButton.titleLabel?.text
firstButton.setTitle(secondButton.titleLabel?.text, for: .normal)
secondButton.setTitle(firstButtonText, for: .normal)
}

Related

Set up and remove UIButton text label

I'm trying to realize tik tak toe game. So, I have 9 buttons and every time I'm pressing on them they install their text label as "X" or "O"
sender.setTitle("X", for: .normal)
// or
sender.setTitle("O", for: .normal)
But then , when game is finished , I want to delete all text labels and facing a problem - I can't remove text labels. I've tried several variants and still can't understand problem. I tried :
button.setTitle(nil, for: .normal)
button.setTitle("", for: .normal)
button.titleLabel?.text = ""
button.titleLabel?.text = nil
It's not working. Even if I don't see text at this buttons after my "failed reset", text is still set.
Even when I'm doing all variants to delete text and then calling
button.titleLabel?.text
Im getting not empty line or nil, Im getting "X"!!! (if there was "x" text before)
i checked it ... these all lines work .. problem is with your connection
#IBOutlet weak var button: UIButton!
override func viewDidLoad() {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.button.setTitle("", for: .normal)
//self.button.backgroundColor = .red
}
super.viewDidLoad()
// Do any additional setup after loading the view.
}

Customise tool bar item with label and image?

I am trying to do a custom toolbar that shows image and label but changes according to which view it is in. I have scoured the internet and read and tried everything I could find on the subject.
I figured that putting a tab bar was not a solution since it can't change for every view (or can it?).
Last time I asked I was marked duplicate and quoted Set image and title for bad button item?. I tried that but I could not get that to work at all. Ideally, I wanted a storyboard solution.
I'll show what I have done and tried with so far. What am I doing wrong? Have I missed something? I can't get the title to show on the toolbar item.
Xcode main.storyboard current toolbar version:
Sketch version (what I want it to look like):
What I hope it will change to in subsequent view:
Code:
class ViewController: UIViewController {
#IBOutlet weak var toolbar: UIToolbar!
#IBOutlet weak var leftBarButton: UIBarButtonItem!
var middleBarButton: UIBarButtonItem!
var rightBarButton: UIBarButtonItem!
var rightBarButton2: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
let view = UIView()
let button = UIButton(type: .system)
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "IconHelp"), for: .normal)
button.setTitle("Help", for: .normal)
button.addTarget(self, action: #selector(about), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
// var items = self.toolbar.items
// items![0] = UIBarButtonItem(customView: view)
leftBarButton = UIBarButtonItem(customView: button)
Any help would be much appreciated as I've tried to solve this all week. :P

Why does my UIButton affect my UIImageView in Swift?

I'm building a simple app in Swift, and the app contains a png image, a UISegmentedControl, and a UIButton. Selecting different controls on the UISegmentedControl resizes an image of a pizza. When I click on "Large" in the UISegmentedControl, and then I press a button on the interface called submitOrderDisplay, it shrinks the image from its large size to its small size. I know it has something to do with the setTitle method I am using at the very bottom of the code, but I don't understand why that causes the image to reset to its default small size. If I remove the setTitle method, the resizing does not occur after I click the UIButton. I don't want the image to change size after the button is pressed. How do I achieve that?
import UIKit
class SecondViewController: UIViewController{
#IBOutlet weak var sizeChoiceControl: UISegmentedControl!
#IBOutlet weak var submitOrderDisplay: UIButton!
#IBOutlet weak var myImageView: UIImageView!
#IBAction func sizeSelected() {
if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Small"){
myImageView.setHeight(height: 85)
myImageView.setWidth(width: 85)
myImageView.setX(x: 146)
myImageView.setY(y: 63)
}
else if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Medium"){
myImageView.setHeight(height: 100)
myImageView.setWidth(width: 100)
myImageView.setX(x: 139)
myImageView.setY(y: 55)
}
else if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Large"){
myImageView.setHeight(height: 115)
myImageView.setWidth(width: 115)
myImageView.setX(x: 131)
myImageView.setY(y: 48)
}
}
#IBAction func SubmitOrderClicked(sender: UIButton) {
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Normal)
}
}
Write the configuration of the button inside the viewDidLoad function:
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Normal)
I understand that you want to put the title Update Order.
If you want to change the title when pressed, you must call it for another State of the button like:
submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Highlihgted)
If you change the configuration of the button for different states when an event happens, that must be why the size is ressetting.

Implementing UIButtons for selection in Swift

I'm new to iOS development and I'm working on a Fire Safety App. I would like to give the user the option of 3 risk levels: Low, Medium and High by using (I suppose) 3 UIButtons which the user can tap, and which ever one they tap would turn a certain colour.
I've managed to get 3 UIButtons working, and they can change colour when tapped but if I tap another I can't get the previous button to change back and I've had difficulties getting a value for them (For example when submitted if the user pressed Low I would use the number 2 for calculations)
To show this I did a quick drawing:
Button Example
Thanks for your help :)
==== EDIT ====
I am now using a Segmented Control instead of UIButtons to do this. But I need to know how to use them in an if statement
#IBOutlet var RiskChoice: UISegmentedControl!
#IBOutlet var ValueLabel: UILabel!
#IBOutlet var CalcButton: UIButton!
#IBAction func Calculate(sender: UIButton) {
if (RiskChoice.selectedSegmentIndex == 0){
ValueLabel.text = String("Low")
}
Nothing appears in the Label I have set up.. Could someone direct me here?
You can just reset all the 3 buttons to their default state when any of the button is tapped and then highlight the current button.
#IBAction func buttonClicked(sender: UIButton) {
/** Reset all button to default state*/
resetAllButtons()
/** Highlight current button */
sender.backgroundColor = UIColor.redColor()
sender.titleColor = UIColor.whiteColor()
}
private func resetAllButtons() {
button1.backgroundColor = UIColor.clearColor()
button1.titleColor = UIColor.blackColor() /// Or use this : button1.setTitleColor(UIColor.blackColor, forState: .Normal)
button2.backgroundColor = UIColor.clearColor()
button2.titleColor = UIColor.blackColor()
button3.backgroundColor = UIColor.clearColor()
button3.titleColor = UIColor.blackColor()
}

Swift - Assign titles to multiple buttons

I have multiple buttons that all get their titles from an array. I would like to be able to assign the title with a loop, but I can't figure how to refer to each button as I go through the loop.
Currently I am adding each title with a line of code like this:
button0.setTitle(title[0], forState: .Normal)
button1.setTitle(title[1], forState: .Normal)
button2.setTitle(title[2], forState: .Normal)
button3.setTitle(title[3], forState: .Normal)
etc...
I have added an IBOutlet to each button, but I am also using tags for another purpose, so if there is a way to use tags to assign the titles, I would be happy to do that.
Any thoughts?
You need an IBOutletCollection
In your Swift ViewController, assign all your buttons to the below
#IBOutlet var buttons: [UIButton]!
Then to assign the titles
var buttonTitles = ["Button1","Button2"]
for (index,button) in buttons.enumerate()
{
if buttonTitles.count > index
{
if let title : String = buttonTitles[index]
{
button.setTitle(title, forState: .Normal)
}
}
}

Resources