【问题标题】:Linking two (or more) UISegmented Controls链接两个(或更多)UISegmented 控件
【发布时间】:2020-06-03 11:43:15
【问题描述】:

如果选择的答案是No,我正在尝试从我的第一个分段控件中获取答案,然后自动选择第二个(以及更多)分段控件的答案。

所以Question1分段控制(是,否,N/A),如果选择No,则对Question2(和Q3,Q4等)分段控制作出答复N/A。 我可以让它单独在Question1上工作,即按No但在Question1上更改为N/A,但我希望它离开Question1No,然后将Question2等更改为N/A.

我尝试将sender 更改为Question2,但这不起作用。

        @IBOutlet weak var Queston2: UISegmentedControl! //Will need to add same for Q3 etc

        @IBAction func Question1(_ sender: UISegmentedControl) {

            if sender.selectedSegmentIndex == 0 {
            //Do stuff
            }

            else if sender.selectedSegmentIndex == 1 {

                sender.selectedSegmentIndex = 2                  // Change button to 'N/A', works for this question, but i want it to change Question2, not Question1.

         @IBAction func Question2(_ sender: UISegmentedControl) { // generally repeats as Question1 above.

我找到了一些类似的答案,但它们似乎比我认为需要的要复杂得多。

【问题讨论】:

  • 所以 - 你有 nUISegmentedControl 对象,所有对象都带有“是 - 否 - 不适用”,当在其中任何一个上选择“否”时,所有其他都应该设置为“N/A”吗?但是,如果选择是或不适用,是否要执行其他操作?

标签: ios swift uisegmentedcontrol


【解决方案1】:

这基本上是你可以做的:

class ArrayOfSegsViewController: UIViewController {

    @IBOutlet var q1SegControl: UISegmentedControl!
    @IBOutlet var q2SegControl: UISegmentedControl!
    @IBOutlet var q3SegControl: UISegmentedControl!
    @IBOutlet var q4SegControl: UISegmentedControl!
    @IBOutlet var q5SegControl: UISegmentedControl!


    @IBAction func q1SegControlChangeed(_ sender: UISegmentedControl) {

        if sender.selectedSegmentIndex == 0 {
            // do stuff
        } else if sender.selectedSegmentIndex == 1 {
            // set all others to "N/A"
            [q2SegControl, q3SegControl, q4SegControl, q5SegControl].forEach {
                $0.selectedSegmentIndex = 2
            }
        } else {
            // do stuff
        }

    }

}

== 1 块中,您创建一个由其他分段控件组成的数组并循环执行,说“对于每个控件,将选定的分段索引设置为 2。”

旁注:使用

lowerCaseFirstChar

用于变量和函数名称。使用

UpperCaseFirstChar 

用于类/结构/枚举/等。

【讨论】:

  • @ColinD - 你是在 Storyboard 中把这些都布置好了吗?还是通过代码创建它们?无论哪种方式,有这么多,你可能会更好地将它们放在一个数组中。然后你可以使用arrayOfControls.forEach
  • 我已经在 Storeyboard 中设置了这个,现在可以正常使用了。
  • @ColinD - 如果这回答了您的问题,请务必将答案标记为已接受,以便其他遇到它的人受益。
  • 谢谢,现在完成,我不知道,因为这是我的第一个问题。谢谢。
猜你喜欢
  • 2012-06-21
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 2014-08-05
  • 2021-04-16
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
相关资源
最近更新 更多