2016-09-26 3 views

答えて

4
let segmentedControl = UISegmentedControl() 
segmentedControl.insertSegment(withTitle: "Title", at: 0, animated: true) 
segmentedControl.setTitle("Another Title", forSegmentAt: 0) 
1

は、私は、@ RyuX51 の溶液を使用して、私の問題を解決し、私のコードは以下のようになります。

class MyCustomViewController: UIViewController{ 

    @IBOutlet weak var ServicesSC: UISegmentedControl! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     ServicesSC.removeAllSegments() 


     ServicesSC.insertSegment(withTitle: "Title", at: 0, animated: true) 
     ServicesSC.setTitle("Another Title", forSegmentAt: 0) 

    } 


} 
2

私が勘違いしておりません場合は、あなたがセグメントに「UISegmentedControl」を追加することを意味インターフェイスビルダを使用せずに、プログラムでコンポーネントを実行できます。

はい、それは可能である:

// Assuming that it is an "IBOutlet", you can do this in your "ViewController": 
class ViewController: UIViewController { 

    @IBOutlet weak var segmentedControl: UISegmentedControl! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // remove all current segments to make sure it is empty: 
     segmentedControl.removeAllSegments() 

     // adding your segments, using the "for" loop is just for demonstration: 
     for index in 0...3 { 
      segmentedControl.insertSegmentWithTitle("Segment \(index + 1)", atIndex: index, animated: false) 
     } 

     // you can also remove a segment like this: 
     // this removes the second segment "Segment 2" 
     segmentedControl.removeSegmentAtIndex(1, animated: false) 
    } 

    // and this is how you can access the changing of its value (make sure that event is "Value Changed") 
    @IBAction func segmentControlValueChanged(sender: UISegmentedControl) { 
     print("index of selected segment is: \(sender.selectedSegmentIndex)") 
    } 
} 
関連する問題