I have searched the web and youtube about segmented control, but I only find examples of how to change to a different view.
My goal is to take the selected value from the segmented control and use it in the action on another button.
I will try to explain what I mean :
#IBAction func segmentetControll(_ sender: UISegmentedControl) {
value 1
value 2
value 3
}
#IBAction func calculateButton(_ sender: Any) {
if value 1 {
do this
}
else if value 2 {
Do that
}
else if value 3 {
Do thids
}
You need to connect it via #IBOutlet to file.
Then you can use its index to do whatever you want.
#IBOutlet var segmentedControl: UISegmentedControl!
#IBAction func calculateButton(_ sender: Any) {
let index = segmentedControl.selectedSegmentIndex
//...
}
Putting the code in the IBAction method connected via the storyboard or Xib file is the correct approach. You can use strong types when hooking up the action to the control and write code such as:
#IBAction func calculateButton(_ sender: UISegmentedControl) {
switch(sender.selectedSegmentIndex){
case 1:
// Do something
case 2:
// Do something
case 3:
// Do something
default:
break
}
}
You would need to create an #IBOutlet var segmentedControl: UISegmentedControl? property that connects to the segmented control in the Interface Builder, and then in the calculateButton method you can switch over segmentedControl.selectedSegmentIndex. Let me know if you need further explanation.
Related
Why when I try to print button title i used print(sender.currentTitel) and isn't working.
And this in below it is work:
print((sender as AnyObject).currentTitle!!)
I assume you are in a IBAction function like this:
#IBAction func buttonTapped(_ sender: Any) {
// print here
}
This is due to the Any reference you declare when you create the IBAction. Two solution.
You can modify your IBAction like this:
#IBAction func buttonTapped(_ sender: UIButton) {
// print(sender.titleLabel?.text)
}
or test the sender conformance:
#IBAction func buttonTapped(_ sender: Any) {
if let button = sender as? UIButton {
// print(button.titleLabel?.text)
}
}
Solution 1 is better if your IBAction is only triggered by button(s)
Solution 2 may be an approach if your IBAction is used by multiple senders
Cheers
The code should work as follows: On one View I click tag button and tagButtonPressed IBAction should set tag variable to sender.tag of the tag button. It works well. Then user should click 'send' button and sharePressed IBAction should use the tag variable which equals to sender.tag of the 'tag' button, and print the updated value of tag variable. Unfortunately it doesn't work.
import UIKit
class ShareViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var tag : Int = 1
#IBOutlet weak var shareTextField: UITextView!
#IBAction func tagButtonPressed(_ sender: UIButton) {
tag = sender.tag // lets say sender.tag = 4
print(tag) // It prints 4
}
#IBAction func sharePressed(_ sender: Any) {
print(tag) // it prints 1 and i want it to print also 4
}
}
Your code is correct. Somewhere else (not shown) you're re-setting the tag value to 1.
I am trying to use a UIButton as a switch. When the button is tapped, the button image should change to (atcs.png) and state should be (.selected).
When image is tapped again, the button image should again to (actns.png) and state should be (.normal).
Below is an example of my current code:
#IBOutlet weak var atcBtn: UIButton!
#IBAction func atcTapped(_ sender: UIButton) {
if atcBtn.isSelected {
atcBtn.setImage(UIImage(named: "atcs.png"), for:.selected)
} else {
atcBtn.setImage(UIImage(named: "atcns.png"), for:.normal)
}
}
This somehow isn't working. Is there anything missing? Tried almost every solution listed on here, but nothing.
First, move your setImage code to viewDidLoad. There's no reason to repeatedly set these images:
atcBtn.setImage(UIImage(named: "atcs.png"), for:.selected)
atcBtn.setImage(UIImage(named: "atcns.png"), for:.normal)
Next, toggle the isSelected property when the button is tapped:
#IBAction func atcTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
}
Also of note - use sender instead of hardcoding atcBtn.
I want to be able to call function start within function use without hitting the action button for start. I know simple thing to do is just put print("a") in use. But I am using this as a simple example because I have a more complex problem in mind.
#IBAction func start(_ sender: Any) {
print("a")}
fun use() {
}
viewdidload() {
use()
}
Consider refactoring your functions. Instead of putting the button action code directly inside of the #IBAction function, put it in a separate function. This way, you can call this code from multiple places.
Here is one solution:
#IBAction func start(_ sender: Any) {
startAction()
}
func startAction() {
print("a")
}
func use() {
startAction()
// anything else
}
override func viewDidLoad() {
use()
}
Create an IBOutlet for your button:
#IBOutlet weak var button: UIButton!
Then simply, use this code to trigger its action:
button.sendActions(for: .touchUpInside)
I need a UISegmentedControl with 2 row, I could not implement it.
I dont want to use 2 different UISegmentedControl. Are there any solution to this problem? How to implement UISegmentedControl with two rows?
'UISegmentedControl' does not support multiple rows. If you need it you'll have to build it yourself or find a third party library that offers it.
As Helium suggests in his/her comment, you could create a custom control that internally manages 2 or more segmented controls and handles changing states between them.
Truth be told a segmented control is not that complicated. It wouldn't be that hard to create a custom 2D segmented control. If this is a key need then you might want to go that route.
Basically, you will need to have an IBAction and IBOutlet on each of your segmented control.
Code the IBAction such that clicking on one of the segmented control will set the IBOutlet's selectedSegmentIndex of the other segmented controls(which are not selected) to UISegmentedControlNoSegment. This creates the illusion that the segmented controls are linked.
Then getting the correct info of which segmented control value is selected is simply gathering all the index of the segmented control into one constant.
#IBOutlet weak var segment1: UISegmentedControl!
#IBOutlet weak var segment2: UISegmentedControl!
#IBOutlet weak var segment3: UISegmentedControl!
#IBAction func FirstSecond(_ sender: UISegmentedControl) {
segment2.selectedSegmentIndex = UISegmentedControlNoSegment
segment3.selectedSegmentIndex = UISegmentedControlNoSegment
}
#IBAction func ThirdFourth(_ sender: UISegmentedControl) {
segment1.selectedSegmentIndex = UISegmentedControlNoSegment
segment3.selectedSegmentIndex = UISegmentedControlNoSegment
}
#IBAction func FifthSixth(_ sender: UISegmentedControl) {
segment1.selectedSegmentIndex = UISegmentedControlNoSegment
segment2.selectedSegmentIndex = UISegmentedControlNoSegment
}
let segmentvalue = ["First","Second","Third","Fourth","Fifth","Sixth"]
#IBAction func FirstSecond(_ sender: UISegmentedControl) {
segment2.selectedSegmentIndex = UISegmentedControlNoSegment
segment3.selectedSegmentIndex = UISegmentedControlNoSegment
let valueINeed = segmentvalue[segment1.selectedSegmentIndex]
}
#IBAction func ThirdFourth(_ sender: UISegmentedControl) {
segment1.selectedSegmentIndex = UISegmentedControlNoSegment
segment3.selectedSegmentIndex = UISegmentedControlNoSegment
let valueINeed = segmentvalue[segment2.selectedSegmentIndex + 2]
}
#IBAction func FifthSixth(_ sender: UISegmentedControl) {
segment1.selectedSegmentIndex = UISegmentedControlNoSegment
segment2.selectedSegmentIndex = UISegmentedControlNoSegment
let valueINeed = segmentvalue[segment3.selectedSegmentIndex + 4]
}
Full Details availabe here: multi row segemented control in swift