How to change button destiny with segmented control?

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?

Answered by Claude31 in 275298022

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
        }
    }

What do mean:

On the same page, I have a button leading to the next page

So I understand the button and segmented control are on the same view

but

My segemented control is located in my ViewController.swift and my button in Main.storyboard.


storyboard is a set of views. The button must be in a view. Which one is it ?


In addition, I suppose you have declare an IBOutlet for segmented control, as

@IBOutlet var Sege : UISegmentedControl!

Did you do it ?

Note that by convention, the name should start with a lowercase sege

Hi Claude, thanks for your help!

Yea I did it like this:


@IBOutlet weak var Sege: UISegmentedControl!


Also the button is on the same view as the segmented control.

Accepted Answer

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
        }
    }

Hey, sorry that it took me so long to answer, but I have a problem and tried to figure it out.

I connected my first view to 3 different views, as you said, and named them "FirstSegue", "SecondSegue" and "ThirdSegue".

When I start my App now, the first value of the segmented view is selected, and the button to to the "FirstSegue" view, as it should, but when i just click on the second or third value, the app crashes and I get an error "Thread 1: signal SIGABRT".

According to the internet, I should delete onee "Referencing Outlet", but except for the segmented control there is one. If I delete the one from the segmented control nothing works. Do you know where the problem is?

Figured it out and now it works, thanks a lot!

How to change button destiny with segmented control?
 
 
Q