我有一个分为两个部分的分段控制动作方法
@IBAction func segmentChanged(_ sender: UISegmentedControl) {
// What goes here for displaying data similar to buttonsTapped?
}
我有以下三个按钮
@IBOutlet weak var buttonOne: UIButton!
@IBOutlet weak var buttonTwo: UIButton!
@IBOutlet weak var buttonThree: UIButton!
我对所有3个按钮都有一种操作方法,
@IBAction func buttonsTapped(_ sender: UIButton) {
switch sender {
case buttonOne:
print("Button one Tapped")
case buttonTwo:
print("Button two tapped")
case buttonThree:
print("Button three tapped")
default: break
}
if segmentedControl.selectedSegmentIndex == 0
{
print("First Segment")
}
else
{
print("Second Segment")
}
}
当我点击buttonOne时,我得到的是"First Segment"
和"Button One Tapped"
,就像在第一段中一样。同样,buttonTwo和buttonThree在控制台上显示正确的消息。
但是当我更改分段控件时,控制台上没有任何消息。我通过在buttonOne.sendActions(for: .touchUpInside)
方法中调用segmentChanged
进行了尝试,该方法仅适用于buttonOne并弄乱了其他按钮。更改细分后,如何获得所有按钮和细分的正确消息?预先感谢。
答案 0 :(得分:3)
您需要为if (dummyObject.arr){
console.log('arr is present')
}else{
console.log('arr is not present')
}
而不是UISegmentedControl
设置.valueChange
方法。
看起来如下:
并添加以下方法。
代码:
.touchUpInside
答案 1 :(得分:1)
使用以下方法记录细分控制值
@IBAction func segmentChanged(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0
{
print("First Segment")
}
else
{
print("Second Segment")
}
}
然后使用以下方法记录按钮操作:
@IBAction func buttonsTapped(_ sender: UIButton) {
switch sender {
case buttonOne:
print("Button one Tapped")
case buttonTwo:
print("Button two tapped")
case buttonThree:
print("Button three tapped")
default: break
}
}
这两种方法无法合并,因为与事件内部的触摸相关的按钮和分段控件与值更改的事件相关联