如何在swift中更改segmentController的索引。我想在单击按钮时应更改段索引的值
func respondToGesture(sender: UIGestureRecognizer)
{
if let swipeGesture = sender as? UISwipeGestureRecognizer
{
if swipeGesture.direction == UISwipeGestureRecognizerDirection.Right
{
// change the segment to previous
}
else
{
// change th esegment to next
}
}
}
答案 0 :(得分:3)
以下是为段设置所选索引的方法:
import UIKit
import AVFoundation
class ViewController: UIViewController {
//outlet of your segment
@IBOutlet weak var segment: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func setOne(sender: AnyObject) {
//set selected index to 0
segment.selectedSegmentIndex = 0
}
@IBAction func setTwo(sender: AnyObject) {
//set selected index to 1
segment.selectedSegmentIndex = 1
}
@IBAction func setThree(sender: AnyObject) {
//set selected index to 2
segment.selectedSegmentIndex = 2
}
@IBAction func four(sender: AnyObject) {
//set selected index to 3
segment.selectedSegmentIndex = 3
}
}
结果将是:
希望它有所帮助。
答案 1 :(得分:0)
你可以这样做:
@IBOutlet weak var mySegmentControl: UISegmentedControl!
var current = 0 // For current selection
@IBAction func buttonPressed(sender: AnyObject) {
if current == 0 {
mySegmentControl.selectedSegmentIndex = 1
current = 1
} else {
mySegmentControl.selectedSegmentIndex = 0
current = 0
}