[swift] Segment 簡易建立

Jacob 黃炬楷
Jul 28, 2017 · 2 min read

建立IBOutlet,名稱: genderSegment

@IBOutlet weak var genderSegment: UISegmentedControl!

在viewDidLoad方法內,寫入addTarget方法,

genderSegment.addTarget(self, action: #selector(self.genderSelected(_:)), for: .valueChanged)

當要取得使用者選哪一個Segment時,可以用.selectedSegmentIndex

func genderSelected(_ sender: UISegmentedControl) {switch genderSegment.selectedSegmentIndex {case 0:genderLabel = "Male"case 1:genderLabel = "Female"default:print("error")}}
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade