简体   繁体   English

如何设置 UISegmentedControl 的默认状态?

[英]How can I set the default state of a UISegmentedControl?

Is there a way to set the starting selected segment in a UISegmentedControl in Interface Builder, or do I have to do it in the code?有没有办法在界面生成器的 UISegmentedControl 中设置起始选定段,或者我必须在代码中设置? If it's in the code, is viewDidLoad the best place to set it?如果它在代码中,viewDidLoad 是设置它的最佳位置吗?

From code, you can just do self.segmentedControl.selectedSegmentIndex = someDefaultIndex .从代码中,你可以只做self.segmentedControl.selectedSegmentIndex = someDefaultIndex

Whether you should set it in viewDidLoad: or not depends entirely on the structure of your application.您是否应该在viewDidLoad:设置它完全取决于您的应用程序的结构。 For example, if your app is starting up and loading the view for the first time and needs to set the control to whatever value it had during the previous run of the app, then it definitely makes sense to do it there.例如,如果您的应用程序是第一次启动并加载视图,并且需要将控件设置为它在应用程序上一次运行期间拥有的任何值,那么在那里进行操作肯定是有意义的。

在界面生成器中,当您在 UI 上选择 UISegmentedControl 对象时,然后在属性窗格中,在段控件中有段下拉菜单,选择要选择的段(0,1 等)并勾选其下方的“已选择”选项。

Select default value for your UISegmentedControl via storyBoard通过 storyBoard 为 UISegmentedControl 选择默认值

  1. Open ur storyboard and select the UISegmentedControl打开你的故事板并选择 UISegmentedControl

在此处输入图片说明

  1. Select atributes inspector of your UISegmentedControl (in the right corner)选择 UISegmentedControl 的属性检查器(在右上角)

在此处输入图片说明

  1. Search the 'segment' atribute and open de dropdown.搜索“段”属性并打开下拉菜单。

在此处输入图片说明

  1. Select the item you want to be selected by default.选择您要默认选择的项目。

在此处输入图片说明

  1. Mark the selected checkbox to set selected by default.选中选中的复选框以设置默认选中。

在此处输入图片说明

If you don't use storyboards and want to set a default index after some setup/networking like me, this little snippet will select something if the user hasn't.如果您不使用故事板并且想像我一样在一些设置/网络之后设置默认索引,那么如果用户没有,这个小片段会选择一些东西。 I placed this in my subclass of UISegmentedControl , but you could place this anywhere.我把它放在我UISegmentedControl子类中,但你可以把它放在任何地方。 ( Swift 3 ) (斯威夫特 3 )

Decl: var UISegmentedControlNoSegment: Int { get }
Desc: A segment index value indicating that there is no selected segment. See selectedSegmentIndex for further information.

Short version:精简版:

if selectedSegmentIndex == UISegmentedControlNoSegment {
    selectedSegmentIndex = initialIndex
}

Longer version更长的版本

func reloadData() {
    guard let numberOfItems = dataSource?.numberOfItems() else {
        return
    }

    removeAllSegments()

    for index in 0...numberOfItems {
        insertSegment(with: $image, at: index, animated: false)
    }

    if selectedSegmentIndex == UISegmentedControlNoSegment {
        selectedSegmentIndex = initialIndex
    }
}

After clicking the Segmented Control go to where you created the segments and choose the one you want be default.单击分段控件后,转到创建分段的位置并选择您想要的默认分段。 Then below that there will be a Box with "Selected" by it.然后在它下面会有一个带有“已选择”的框。 Select that and it will be default.选择它,它将是默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM