ios 分段控制上的快速处理动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30545198/
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-08-31 06:08:22  来源:igfitidea点击:

Swift handle action on segmented control

iosswiftuisegmentedcontrol

提问by Poonam

I have a HMSegmentedControl with 4 segments. When it is selected, it should pop up view. And when the pop up dismissed, and trying to click on same segment index it should again show the pop up. By using following does not have any action on click of same segment index after pop up dissmissed.

我有一个带有 4 个段的 HMSegmentedControl。当它被选中时,它应该弹出视图。当弹出窗口关闭并尝试单击相同的段索引时,它应该再次显示弹出窗口。弹出关闭后,通过使用以下对同一段索引的单击没有任何操作。

segmetedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents: UIControlEvents.ValueChanged) 

采纳答案by Icaro

You set your target to fire just when the value change, so if you select the same segment the value will not change and the popover will not display, try to change the event to TouchUpInside, so it will be fired every time you touch inside the segment

您将目标设置为仅在值更改时触发,因此如果您选择相同的段,则该值不会更改并且不会显示弹出框,请尝试将事件更改为 TouchUpInside,因此每次您触摸内部时都会触发它部分

segmetedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents:.TouchUpInside) 

回答by Arbitur

You can add the same target for multiple events.

您可以为多个事件添加相同的目标。

So lets say your segmentedControlValueChanged:looks like this:

所以让我们说你的segmentedControlValueChanged:样子:

func segmentedControlValueChanged(segment: UISegmentedControl) {
    if segment.selectedSegmentIndex == 0 {
    }
    ...
}

Then you can add targets for more than 1 events to call this function:

然后你可以为 1 个以上的事件添加目标来调用这个函数:

segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents:.ValueChanged)
segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents:.TouchUpInside)

Now your function will get called when a value was changed and when the user releases his finger.

现在,您的函数将在值更改和用户松开手指时被调用。

回答by Kris Roofe

with sender, use the sender name sender when you want to access in the action:

对于发件人,当您要在操作中访问时使用发件人名称发件人:

segmentControl.addTarget(self, action: #selector(changeWebView(sender:)), for: .valueChanged)

or

或者

addTarget(self, action: #selector(changeWebView), for: .valueChanged)

回答by ikbal

Swift 5

斯威夫特 5

// add viewController

// 添加视图控制器

@IBOutlet var segmentedControl: UISegmentedControl!

override func viewDidLoad() {
    super.viewDidLoad()
    segmentedControl.addTarget(self, action: #selector(CommentsViewController.indexChanged(_:)), for: .valueChanged)
}

// using change

// 使用更改

@objc func indexChanged(_ sender: UISegmentedControl) {
    if segmentedControl.selectedSegmentIndex == 0 {
        print("Select 0")
    } else if segmentedControl.selectedSegmentIndex == 1 {
        print("Select 1")
    } else if segmentedControl.selectedSegmentIndex == 2 {
        print("Select 2")
    }
}

回答by Dnyaneshwar Shinde

@IBAction func segmentedControlButtonClickAction(_ sender: UISegmentedControl) {
   if sender.selectedSegmentIndex == 0 {
      print("First Segment Select")
   }
   else { 
      print("Second Segment Select")
   }
}

回答by Arunabh Das

Swift4 syntax :

Swift4 语法:

segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", for:.touchUpInside)