0

I want to make the segment controller must be selected by user but i don't know how to detect if he choose a button on segment controller or not?

I'm trying to write:

if ( segmentbtn != nil)
{
  Println(" You must select!")
}

but isn't working, i'm using Xcode on swift language.

iBhavin
  • 1,261
  • 15
  • 30
Salah
  • 933
  • 3
  • 13
  • 32

1 Answers1

0
@IBOutlet weak var segmentbtn: UISegmentedControl!

@IBAction func indexChanged(sender: UISegmentedControl) {
    switch segmentbtn.selectedSegmentIndex {
    case 0:
        println("button 1 is selected")
    case 1:
        println("button 2 is selected")
    default:
        break;
    }
}
manc1n1
  • 72
  • 9
  • isn't working and i'm getting breakpoint, in signup button i want to check if the segment enabled or not, if its enabled then let him signup if not make UIAlertView , I tried this : `if (segmentbtn != segmentbtn.isEnabledForSegmentAtIndex(0)) { //alertview }` can you help me how to fix this ? – Salah Jul 30 '15 at 13:41
  • @Salah I edited my answer a little, I don't really understand what you exactly want so I hope this helps or maybe you could explain a little more. – manc1n1 Jul 30 '15 at 18:20
  • Hello again, i have a controll view which is for Signup in my app, in the signup page there is a segment controller, I want the app never let him signup by pressing on signup button if he didn't choose a button on segment! like in signup action: if ( segment != enabled){ // show alertview "Sorry choose a segment button" } else { //register him in my app} hope you got it! – Salah Jul 30 '15 at 19:49
  • A segment button is for switching between views. It is not a button. @Salah – manc1n1 Jul 30 '15 at 19:58
  • That's mean can't detect if he enabled index 0 or 1 ? in a signup button action. – Salah Jul 30 '15 at 20:02
  • Use the `Switch` button, it allows you to display an element showing the `Boolean` state of a value. @Salah – manc1n1 Jul 30 '15 at 20:06