Swift3 從入門到精通Day12:UIAlertController

Alice
Daily Swift
Published in
8 min readMay 21, 2017

時間:5/21(日)

內容:

  • 89. 警告控制器 UIAlertController
  • 90. 幫警告控制器加上按鈕9:51
  • 91. 顯示圖片 UIImageView7:41
  • 92. 用程式碼換圖3:10
  • 93. 按鈕 UIButton6:19
  • 94. 用程式碼產生按鈕10:58
  • 95. 選單 PickerView8:05
  • 96. 設定選單的選項9:19
  • 97. 複習與總結 UIPickerView3:05
  • 98. 協定 Protocol8:57
  • 99. 解釋 UIPickerViewDelegate 的原理6:34
  • 100. 解釋 UIPickerViewDataSource 的原理3:46
  • 101. 協定導向程式設計 Protocol Oriented Programming9:19
  • 89. 警告控制器 UIAlertController

@IBAction func showAlert(_ sender: UIButton) {

let myAlert = UIAlertController(title: “Hello”, message: “How are you”, preferredStyle: .alert)

present(myAlert, animated: true, completion: nil)

}

  • 90. 幫警告控制器加上按鈕9:51

import UIKit

class ViewController: UIViewController {

@IBAction func showAlert(_ sender: UIButton) {

let myAlert = UIAlertController(title: “Hello”, message: “How are you”, preferredStyle: .actionSheet)

let OKAction = UIAlertAction(title: “Ok”, style: .default) {

(action:UIAlertAction) in

self.dismiss(animated: true, completion: nil)

}

myAlert.addAction(OKAction)

let HelloAction = UIAlertAction(title: “Say Hello”, style: .cancel) {

(action:UIAlertAction) in

print(“Hello”)

self.dismiss(animated: true, completion: nil)

}

myAlert.addAction(HelloAction)

let cancelAction = UIAlertAction(title: “cancel”, style: .destructive) {

(action:UIAlertAction) in

print(“Hello”)

self.dismiss(animated: true, completion: nil)

}

myAlert.addAction(cancelAction)

present(myAlert, animated: true, completion: nil)

}

頁面呈現方式

preferredStyle: .actionSheet

preferredStyle: .alert

字型(影響顏色、粗體)

style: .cancel

style: .destructive

style: .default

  • 91. 顯示圖片 UIImageView7:41

Scale To Fill 失真放大

Aspect TO Fit 任一側到底

Aspect TO Fill 兩側到底不失真

  • 92. 用程式碼換圖3:10

myPet.image = UIImage(name: “MyCat”)

就可以把狗的照片換成貓了(但也要先匯入貓的圖片)

  • 93. 按鈕 UIButton6:19

button旁邊用image找到選PlayButton

然後

調整成highlight按下按鈕之後顯示PlayButtonPressed圖示

  • 94. 用程式碼產生按鈕10:58

95. 選單 PickerView8:05

import UIKit

class ViewController: UIViewController,UIPickerViewDataSource{

//how many component in pickerview

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 2

}

//how many row in component

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

if component == 0{

return 2

}else{

return 4

}

}

第一個return 2=兩排字

第二個return 2=第一排兩個選項

return 4第二排四個選項

  • 96. 設定選單的選項9:19

delegate也是連到view controler

import UIKit

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate{

let numberArray = [“1”,”2",”3",”4",”5",”6",”7",”8"]

let fruitArray = [“apple”,”banana”,”moango”,”watermelon”]

//how many component in pickerview

func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 2

}

//how many row in component

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

if component == 0{

return numberArray.count

}else{

return fruitArray.count

}

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

if component == 0 {

return numberArray[row]

}else{

return fruitArray[row]

}

}

  • 97. 複習與總結 UIPickerView3:05
  • 98. 協定 Protocol8:57
  • 99. 解釋 UIPickerViewDelegate 的原理6:34
  • 98. 協定 Protocol8:57
  • 99. 解釋 UIPickerViewDelegate 的原理6:34
  • 100. 解釋 UIPickerViewDataSource 的原理3:46
  • 101. 協定導向程式設計 Protocol Oriented Programming9:19

心得:Protocol好難我看影片我大概每片都重複看了好幾次

有點一知半解的感覺我決定去看實體書再試一次😅

--

--