2016-09-26 1 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)") 
    } 
} 
관련 문제