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

fr0*_*0zt 12 xcode ios swift xcode7

细分名称是Entrada.我这样做:

override func viewDidLoad() {
    super.viewDidLoad()

    Entrada(sender: UISegmentedControl) {
        setTitle("Action 1", forSegmentAtIndex: 0)
        setTitle("Action 2", forSegmentAtIndex: 1)
        setTitle("Action 3", forSegmentAtIndex: 2)
    }
Run Code Online (Sandbox Code Playgroud)

我得到错误......哇.

Bri*_*ian 12

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) 
 } 
Run Code Online (Sandbox Code Playgroud)


vad*_*ian 6

您必须将分段控件连接到IBOutletInterface Builder中,然后才能编写

@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) 
} 
Run Code Online (Sandbox Code Playgroud)

在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) 
} 
Run Code Online (Sandbox Code Playgroud)