UISegmentedControl প্রোগ্রাম্যাটিক্যালি যোগ করতে সেগমেন্ট আইটেম এবং একশন (action) তৈরি করুন।
func addControl() {
let segmentItems = ["First", "Second"]
let control = UISegmentedControl(items: segmentItems)
control.frame = CGRect(x: 10, y: 250, width: self.view.frame.width - 20, height: 50)
control.addTarget(self, action: #selector(segmentControl(_:)), for: .valueChanged)
control.selectedSegmentIndex = 1
view.addSubview(control)
}
@objc func segmentControl(_ segmentedControl: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0: print("First segment tapped")
case 1: print("Second segment tapped")
default: break
}
}
সিমুলেটরে আউটপুট —
