IOS 分段控制:无法更改选定的分段 [英] IOS Segmented Control: Can't Change Selected Segment

查看:18
本文介绍了IOS 分段控制:无法更改选定的分段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在故事板中创建了一个分段控件,如下所示:

I created a segmented control in my storyboard that looks like this:

然后我为控件的值更改创建了一个 IBAction:

Then I created an IBAction for when the control's value changes:

@IBAction func segmentValChanged(_ sender: UISegmentedControl) {
    print("touched")
    if (sender.selectedSegmentIndex == 0) {
        sender.selectedSegmentIndex = 1;
    }
    else{
        sender.selectedSegmentIndex = 0;
    }
}

唯一的问题是这个函数从来没有被调用过!当我点击笑话(测试版)按钮时,没有任何反应.

The only issue is that this function is never called! When I click the Jokes (Beta) button, nothing happens.

但是,当我为控件创建一个 IBOutlet 并尝试更改我的 viewDidLoad() 中的选定段时,它起作用了:

However, when I created an IBOutlet for the control and tried to change the selected segment in my viewDidLoad(), it worked:

segmentedController.selectedSegmentIndex = 1

我知道我可能错过了一些明显的东西,因为我以前从未使用过分段控件.非常感谢,如果有人能指出这是什么.干杯!

I know I probably missed something obvious, since I've never used a segmented control before. Thanks so much if anyone can point out what this is. Cheers!

import UIKit
import Alamofire

class ViewController: UIViewController {

    var currentImage: CurrentImage!

    var parameters: Parameters!

    @IBOutlet weak var segmentedController: UISegmentedControl!


    @IBAction func segmentValChanged(_ sender: UISegmentedControl) {
        print("touched")
        if (sender.selectedSegmentIndex == 0) { // switch to 1
            sender.selectedSegmentIndex = 1;
        }
        else{
            sender.selectedSegmentIndex = 0;
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        segmentedController.selectedSegmentIndex = 1
        self.segmentedController.addTarget(self, action: #selector(segmentValChanged(_:)), for: .valueChanged)
    }
}

这就是我右键单击视图时的样子:

And this is what it looks like when I right click the view:

推荐答案

每当我看到这个问题时,它几乎总是相同的问题...如果分段控件被绘制在其父视图的之外,则它不会响应点击.这很可能是您遇到的问题.(这也可能发生在 UIButton 上.)

Whenever I see this it is almost always the same problem... If the segmented control is being drawn outside of its parent view, then it will not respond to taps. This is most likely the problem you are having. (This can also happen with UIButtons.)

测试这是否是问题的方法:

Ways to test if this is the problem:

  1. 将分段控件的父级的 clipsToBounds 设置为 true.如果控件不再显示在屏幕上,则它是在其父视图之外绘制的.segmentedControl.superview?.clipsToBounds = true

  1. set clipsToBounds of the segmented control's parent to true. If the control no longer shows on the screen, then it is being drawn outside of it's parent view. segmentedControl.superview?.clipsToBounds = true

为分段控件的父级添加边框.如果控件被绘制在边界外,那么你就可以了.segmentedControl.superview?.layer.borderWidth = 1

add a border to the segmented control's parent. If the control is being drawn outside the border then there you go. segmentedControl.superview?.layer.borderWidth = 1

通过扩大父视图的大小来解决问题,以便在其中绘制控件.

Solve the problem by expanding the size of the parent view so the control is being drawn within it.

附带说明,您的 IBAction 不正确.当调用值更改操作时,分段控件将已经更改其值,并且新的段将突出显示.您不必手动执行此操作.这是另一个指示符,表明您的控件未在其父视图的边界内绘制.

On a side note, your IBAction is incorrect. When the value changed action is called, the segmented control will already have changed its value and the new segment will be highlighted. You don't have to do it manually. This is another indicator that your control isn't being drawn in the bounds of its parent view.

这篇关于IOS 分段控制:无法更改选定的分段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆