如何在同一个UIViewController中使用两个分段控件?l IOS SWIFT3

时间:2017-04-21 06:42:38

标签: ios swift3

在表格视图中,我可以通过这样做选择多个数据源 if(tableView == self.tableOutletName)

但是当谈到分段我该怎么办? 这里我有3个segmentControls

@IBOutlet weak var segmentView1: SJFluidSegmentedControl!

@IBOutlet weak var segmentView2: SJFluidSegmentedControl!

@IBOutlet weak var segmentView3: SJFluidSegmentedControl!

我将如何重写此功能

func numberOfSegmentsInSegmentedControl(_ segmentedControl: SJFluidSegmentedControl) -> Int 

{

    return 6

}

这样我就可以分别给每个段控制6个4和3个段。

此处更新的代码我遇到致命错误

导入基金会 导入UIKit

导入SJFluidSegmentedControl

class DataSource2:UIViewController,SJFluidSegmentedControlDataSource {     @IBOutlet weak var segmentView1:SJFluidSegmentedControl!

@IBOutlet weak var segmentView2: SJFluidSegmentedControl!

@IBOutlet weak var segmentView3: SJFluidSegmentedControl!
func numberOfSegmentsInSegmentedControl(_ segmentedControl: SJFluidSegmentedControl) -> Int {


    switch segmentedControl {
    case segmentView1: return 6
    case segmentView2: return 4
    case segmentView3: return 3
    default: return 3
    }


}
func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                      titleForSegmentAtIndex index: Int) -> String? {
    if index == 0 {
        return "None".uppercased()
    } else if index == 1 {
        return "1 Hrs".uppercased()
    }
    else if index == 2 {
        return "2 Hrs".uppercased()
    }
    else if index == 3 {
        return "3 Hrs".uppercased()
    }
    else if index == 4 {
        return "12 Hrs".uppercased()
    }
    else if index == 5 {
        return "24 Hrs".uppercased()
    }
    return "0".uppercased()
}

func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                      gradientColorsForSelectedSegmentAtIndex index: Int) -> [UIColor] {
    switch index {
    case 0:
        return [UIColor(red: 51 / 255.0, green: 149 / 255.0, blue: 182 / 255.0, alpha: 1.0),
                UIColor(red: 97 / 255.0, green: 199 / 255.0, blue: 234 / 255.0, alpha: 1.0)]
    case 1:
        return [UIColor(red: 227 / 255.0, green: 206 / 255.0, blue: 160 / 255.0, alpha: 1.0),
                UIColor(red: 225 / 255.0, green: 195 / 255.0, blue: 128 / 255.0, alpha: 1.0)]
    case 2:
        return [UIColor(red: 21 / 255.0, green: 94 / 255.0, blue: 119 / 255.0, alpha: 1.0),
                UIColor(red: 9 / 255.0, green: 82 / 255.0, blue: 107 / 255.0, alpha: 1.0)]
    case 3:
        return [UIColor(red: 51 / 255.0, green: 149 / 255.0, blue: 182 / 255.0, alpha: 1.0),
                UIColor(red: 97 / 255.0, green: 199 / 255.0, blue: 234 / 255.0, alpha: 1.0)]
    case 4:
        return [UIColor(red: 227 / 255.0, green: 206 / 255.0, blue: 160 / 255.0, alpha: 1.0),
                UIColor(red: 225 / 255.0, green: 195 / 255.0, blue: 128 / 255.0, alpha: 1.0)]
    case 5:
        return [UIColor(red: 21 / 255.0, green: 94 / 255.0, blue: 119 / 255.0, alpha: 1.0),
                UIColor(red: 9 / 255.0, green: 82 / 255.0, blue: 107 / 255.0, alpha: 1.0)]        default:
        break
    }
    return [.clear]
}

func segmentedControl(_ segmentedControl: SJFluidSegmentedControl,
                      gradientColorsForBounce bounce: SJFluidSegmentedControlBounce) -> [UIColor] {
    switch bounce {
    case .left:
        return [UIColor(red: 51 / 255.0, green: 149 / 255.0, blue: 182 / 255.0, alpha: 1.0)]
    case .right:
        return [UIColor(red: 9 / 255.0, green: 82 / 255.0, blue: 107 / 255.0, alpha: 1.0)]
    }
}
override func viewDidLoad() {
    super.viewDidLoad()
    segmentView1.layer.borderColor = UIColor.blue.cgColor
    segmentView1.layer.borderWidth = 2
    segmentView1.layer.cornerRadius = 25
    segmentView1.clipsToBounds = true

    segmentView2.layer.borderColor = UIColor.blue.cgColor
    segmentView2.layer.borderWidth = 2
    segmentView2.layer.cornerRadius = 25
    segmentView2.clipsToBounds = true

    segmentView3.layer.borderColor = UIColor.blue.cgColor
    segmentView3.layer.borderWidth = 2
    segmentView3.layer.cornerRadius = 25
    segmentView3.clipsToBounds = true
}

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)
}

}

3 个答案:

答案 0 :(得分:1)

您可以switch

switch segmentedControl {
    case segmentView1: return 6
    case segmentView2: return 4
    case segmentView3: return 3
    default: return 0
}

请记住将self设置为所有分段控件的数据源!

答案 1 :(得分:1)

在数据源方法中使用switch / if语句,如下所示:

func numberOfSegmentsInSegmentedControl(_ segmentedControl: SJFluidSegmentedControl) -> Int {
    switch segmentedControl {
    case segmentView1 : 
         return 6
    case segmentView2 : 
         return 4
    case segmentView3 :
         return 3
    default :
         return 0
    }
}

答案 2 :(得分:0)

您始终可以为每个分段控制数据源创建单独的类,而不是将所有分段控制数据源指向同一个视图控制器。它比通过代码使用switch / if / else语句更清晰。