Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting access to a UISegementedControl in Swift

I have been playing with the UISegmentedControl and want to manage it as needed. I've created it on the storyboard, hooked up the outlet and the action. It visualizes fine and the callback on the action indexchanged works. I want to do some other management of this prior to visualization so I am stuck on how to properly access the mysegmentedControl I setup in the storyboard. Seems like a silly question but I could not find a Swift example for the UISegmentedControl that I could relate to currently.

class ViewController: UIViewController, MKMapViewDelegate {
    ...
    //playing around with a Segmented Control
    @IBOutlet var mysegmentedControl : UISegmentedControl

    // Any time the button is clicked in the Segmented Control the index changes so we catch it to do something.

    @IBAction func indexChanged(sender : UISegmentedControl) {
        // This all works fine and it prints out the value of 3 on any click 
        println("# of Segments = \(sender.numberOfSegments)")

        switch sender.selectedSegmentIndex {
        case 0:
            println("first segement clicked")
        case 1:
            println("second segment clicked")
        case 2:
            println("third segemnet clicked")
        default:
            break;
        }  //Switch
    } // indexChanged for the Segmented Control

    override func viewDidLoad() {
        super.viewDidLoad()

         //  1.  How do I get proper access to the mysegmentedControl that was created in the storyboard and is already displayed.
         //      The indexChanged function gets called fine on the display via sender getting passed to it.
         //      I equate this to getting the pointer to it in C so I leverage it elsewhere
         //  2.  Guessing I don't need to init a new UISegmentedControl or do I?

         // This compiles but crashes on run with: fatal error: Can't unwrap 
         println("# of Segments = \(mysegmentedControl.numberOfSegments)")

    } // viewDidLoad    

} // ViewController
like image 471
Kokanee Avatar asked Jul 24 '14 18:07

Kokanee


People also ask

What is UISegmentedControl in Swift?

The UISegmentedControl object automatically resizes segments to fit proportionally within their superview unless they have a specific width set. When you add and remove segments, you can request that the action be animated with sliding and fading effects.

How do you use segmented control in swift 4?

Example Of Segment Control :-Step 3:- Take a segment control into the view Controller and set the constraint in storyboard. Step 4:- Take UIVIew into the view controller and set the constraint on the storyboard. Step 5:- Create the outlet of segment control and UIView in the view controller class.

What is segment in iOS?

The Segment Analytics-iOS SDK can automatically instrument screen calls. It uses method swizzling to detect when ViewController s are loaded, and uses the label of the view controller (or the class name if a label is not available) as the screen name.


2 Answers

Here is what i've used and is working fine for me -

@IBOutlet var mysegmentedControl : UISegmentedControl?

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func indexChanged(sender : UISegmentedControl) {
    // This all works fine and it prints out the value of 3 on any click
    println("# of Segments = \(sender.numberOfSegments)")

    switch sender.selectedSegmentIndex {
    case 0:
        println("first segement clicked")
    case 1:
        println("second segment clicked")
    case 2:
        println("third segemnet clicked")
    default:
        break;
    }  //Switch
} // indexChanged for the Segmented Control

override func viewDidLoad() {
    super.viewDidLoad()
    println("# of Segments = \(mysegmentedControl?.numberOfSegments)")

}
like image 183
Avi Avatar answered Oct 22 '22 02:10

Avi


Please find the below code snippet for accessing the uisegmentedcontrol using swift.

   @IBOutlet weak var segmentedControl: UISegmentedControl!
    
    @IBOutlet weak var textLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        textLabel.text = "First Segment Selected";
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func segmentedControlAction(sender: AnyObject) {
        
        if(segmentedControl.selectedSegmentIndex == 0)
        {
            textLabel.text = "First Segment Selected";
        }
        else if(segmentedControl.selectedSegmentIndex == 1)
        {
            textLabel.text = "Second Segment Selected";
        }
        else if(segmentedControl.selectedSegmentIndex == 2)
        {
            textLabel.text = "Third Segment Selected";
        }
    }

if need detail explanation please refer the below link.

https://sourcefreeze.com/uisegmentedcontrol-example-using-swift-in-ios/

like image 1
Bharathi Devarasu Avatar answered Oct 22 '22 02:10

Bharathi Devarasu