I'm building an app right now and I have a problem with the combination of button and segmented control, if it's even possible.
Right now I have a segemented control, where you can chose between 7 different values.
On the same page, I have a button leading to the next page. Now I want my button to lead to a different page, depending on the selected value in the segemented control.
My segemented control is located in my ViewController.swift and my button in Main.storyboard.
I looked in the code of it and found the destination of the first page the button has to lead to, but I can't figure out how to make it dependent on the segemented control. Simple "if" commands seems not to work in ".storyboard".
I think in the ViewController I could check for the segmentet control with
@IBAction func startRide(_ sender: UIButton) {
switch Sege.selectedSegmentIndex {
case 0 :
UNKNOWN
case 1:
UNKNOWN
case 2:
UNKNOWN
case 3:
UNKNOWN
case 4:
UNKNOWN
case 5:
UNKNOWN
case 6:
UNKNOWN
}
}but I have no idea how to tell the button to lead to another view in the ViewController, only in the storyboard.
Could someone help me out?
So there is no problem to make it work.
You connect the button to the IBAction.
To check, just add at the beginning of the IBAction
print("Test segment value ", sege.selectedSegmentIndex)What is this UNKNOWN in your code ?
To lead to another view, you can use segues.
Create a segue from the first view (the one with segmented control and button) to the destination view by control drag from the yellow button at the top of firstView to the destination (you can have several segues like this).
Name it "FirstSegue" in IB (Identifier of segue) ; it will be used if segment 0 is selected
You should also add a default value:
@IBAction func startRide(_ sender: UIButton) {
print("Test segment value ", sege.selectedSegmentIndex )
switch Sege.selectedSegmentIndex {
case 0 :
performSegue(withIdentifier: "FirstSegue", sender: nil) // can also write sender : sender
case 1:
break
case 2:
break
case 3:
break
case 4:
break
case 5:
break
case 6:
break
default :
break
}
}