點選 Segmented Control 更換圖片

茆閔智
茆閔智
Apr 4, 2020 · 1 min read

程式碼如下:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var outletImageView: UIImageView!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

}

@IBAction func pressSegmentedControl(_ sender: UISegmentedControl) {

if(sender.selectedSegmentIndex == 0){

outletImageView.image = UIImage(named: “0”)

}else if(sender.selectedSegmentIndex == 1){

outletImageView.image = UIImage(named: “1”)

}else if(sender.selectedSegmentIndex == 2){

outletImageView.image = UIImage(named: “2”)

}

}

}

彼得潘的 Swift iOS App 開發教室