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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34454682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:21:16  来源:igfitidea点击:

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

iosxcodeswiftxcode7

提问by fr0zt

The Segment name is Entrada. I am doing like this:

部门名称是 Entrada。我这样做:

override func viewDidLoad() {
    super.viewDidLoad()

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

I get errors... thow.

我收到错误...但是。

回答by Brian

Swift 3.0 use:

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) 
 } 

回答by vadian

You have to connect the segmented control to an IBOutletin Interface Builder, then you can write

您必须将分段控件连接到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) 
} 

In Swift 3+ the syntax has been changed to

在 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) 
} 

回答by Mohammad Kamran Usmani

You can use below code snippet to setup titles of your segment control. Just pass an array of string to set titles.

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

Swift 4+

斯威夫特 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)
    }