当我进入视图控件时,如何更改 3 段控件段的标题?

How can I change the Title for a 3 segmented Control Segments, when I enter a view control?

段名称是 Entrada。 我这样做:

override func viewDidLoad() {
    super.viewDidLoad()

    Entrada(sender: UISegmentedControl) {
        setTitle("Action 1", forSegmentAtIndex: 0)
        setTitle("Action 2", forSegmentAtIndex: 1)
        setTitle("Action 3", forSegmentAtIndex: 2)
    }

我收到错误...不过。

您必须将分段控件连接到 Interface Builder 中的 IBOutlet,然后您可以编写

@IBOutlet var entrada : UISegmentedControl!

override func viewDidLoad() {
  super.viewDidLoad()

  entrada.setTitle("Action 1", forSegmentAtIndex: 0)
  entrada.setTitle("Action 2", forSegmentAtIndex: 1)
  entrada.setTitle("Action 3", forSegmentAtIndex: 2) 
} 

在 Swift 3+ 中,语法已更改为

override func viewDidLoad() {
  super.viewDidLoad()

  entrada.setTitle("Action 1", forSegmentAt: 0)
  entrada.setTitle("Action 2", forSegmentAt: 1)
  entrada.setTitle("Action 3", forSegmentAt: 2) 
} 

Swift 3.0 使用:

 @IBOutlet weak var entrada : UISegmentedControl!

  override func viewDidLoad() {
  super.viewDidLoad()

  entrada.setTitle("Action 1", forSegmentAt: 0)
  entrada.setTitle("Action 2", forSegmentAt: 1)
  entrada.setTitle("Action 3", forSegmentAt: 2) 
 } 

您可以使用下面的代码片段来设置您的段控件的标题。只需传递一个字符串数组即可设置标题。

Swift 4+

func segmentWithTitles(titles : [String])  {
        self.friendsSegment.setTitle(titles[0], forSegmentAt: 0)
        self.friendsSegment.setTitle(titles[1], forSegmentAt: 1)
        self.friendsSegment.setTitle(titles[2], forSegmentAt: 2)
    }