簡體   English   中英

如何快速更改UISegmentController的索引

[英]how to change the index of UISegmentController in 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


                        }

    }

}

您可以通過以下方法為細分設置所選索引:

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
    }
}

結果將是:

在此處輸入圖片說明

希望能幫助到你。

您可以這樣:

@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
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM