swift学习选pizza项目
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
原文:?https://makeapppie.com/2014/09/18/swift-swift-implementing-picker-views/
效果:
步驟:
新建iOS single view application 名字為SwiftPickerViewPizzaDemo, 打開main storyboard選中view controoler,?右上角,?attribute inspector中simulated metrics 的size 選擇iphone 4.7-inch這樣view controller更像是一個(gè)iphone..
然后拖動(dòng)三個(gè)控件到界面上label, label, picker view
最后打開assistant editor, ctrl 拖動(dòng)第二個(gè)label以及picker view控件到viewController.swift中, 會(huì)自動(dòng)生成如下代碼
?
在ViewController中新增如下屬性:
?
讓ViewController實(shí)現(xiàn)兩個(gè)接口.
class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {在viewDidLoad中讓ViewController自身成為picker view的delegate
?
下面實(shí)現(xiàn)接口中定義的方法 以解決如下錯(cuò)誤:?Type 'ViewController' does not conform to protocol 'UIPickerViewDataSource'
// 一共有多少列, 這里有兩列, 一列是size, 一列是toppingfunc numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {return pickerData.count}// 每列有多少條記錄func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {return pickerData[component].count}// 每列中的每行顯示什么內(nèi)容func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {return pickerData[component][row]}// 選中某行時(shí)的回調(diào)函數(shù).func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {updateLabel()}這里可以利用代碼提示,比如實(shí)現(xiàn)最后一個(gè)方法只需要輸入pickerViewdid再自動(dòng)補(bǔ)全就寫好了.
?
完整的代碼如下:
// // ViewController.swift // SwiftPickerViewPizzaDemo // // Created by cyper on 16/6/3. // Copyright ? 2016年 Moaz Tech. All rights reserved. //import UIKitclass ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {// 定義要顯示的兩欄數(shù)據(jù). 第1欄為尺寸, 第2欄為pizza表層的用料// 分別是奶酪, 辣肉腸, 香腸, 蔬菜 和 烤雞let pickerData = [["10\"","14\"","18\"","24\""],["Cheese", "Pepperoni", "Sausage", "Veggie", "BBQ Chicken"]]enum PickerComponent: Int {case size = 0case topping = 1}//MARK -Outlets and Properties@IBOutlet weak var myLabel: UILabel!@IBOutlet weak var myPicker: UIPickerView!//MARK - Instance Methodsfunc updateLabel(){let szComponent = PickerComponent.size.rawValuelet tpComponent = PickerComponent.topping.rawValuelet size = pickerData[szComponent][myPicker.selectedRowInComponent(szComponent)]let topping = pickerData[tpComponent][myPicker.selectedRowInComponent(tpComponent)]myLabel.text = "Pizza: \(size) \(topping)"}//MARK - Life Cycleoverride func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.myPicker.delegate = selfmyPicker.dataSource = self// 默認(rèn)選中18寸的myPicker.selectRow(2, inComponent: PickerComponent.size.rawValue, animated: false)updateLabel()}//MARK - Delgates and Data Source// 一共有多少列, 這里有兩列, 一列是size, 一列是toppingfunc numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {return pickerData.count}// 每列有多少條記錄func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {return pickerData[component].count}// 每列中的每行顯示什么內(nèi)容func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {return pickerData[component][row]}// 選中某行時(shí)的回調(diào)函數(shù).func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {updateLabel()} }
美化應(yīng)用.
1. 將原文中的背景圖photo-sep-14-7-40-59-pm_small1.jpg另存到本地, 然后拖動(dòng)到項(xiàng)目根目錄下(project navigator)
2. 這樣在xcode右下角的media library中就能看到這張圖片
3. 從media library把這張圖片手動(dòng)到view controller里邊, 圖片會(huì)覆蓋整個(gè)手機(jī)屏幕, 從outline中將這個(gè)圖片放到最上面(在outline中越靠近上面的條目用css的術(shù)語來說它的z-index值越小)
4. 選中picker view設(shè)置它的背景色(從顏色選擇器中選擇crayon 模式 , 顏色選?Snow?透明度 50%)
5. 給兩個(gè)label設(shè)置透明的背景, 方法是先拖動(dòng)一個(gè)新的空白view到最下面(如下), 如法炮制設(shè)置它的背景為snow 50%,??然后將最上面的兩個(gè)label拖動(dòng)到這個(gè)空白view里邊, 當(dāng)你把一個(gè)view拖進(jìn)另一個(gè)view的時(shí)候, 這個(gè)view就會(huì)變成subview.
6. 將這個(gè)包含了兩個(gè)label的view拖回到最上面..
作者一再強(qiáng)調(diào), 盡量使用table view而不要使用picker view,?(使用picker view的場(chǎng)景是顯示的內(nèi)容相對(duì)固定, 不超過3欄, 每欄的內(nèi)容不超過15條)
期間碰到了一個(gè)問題: 背景圖片的高度不夠, 導(dǎo)致屏幕下面多出了一片空白, 解決辦法:
1. 選中View Controller, 在file inspector中反選auto layout和 using size class (后來選中也不影響? ?還要繼續(xù)學(xué)習(xí)auto layout的用法)
2. 選中image, 在attribute inspector中設(shè)置view mode為scale to fill
?
轉(zhuǎn)載于:https://my.oschina.net/uniquejava/blog/686626
總結(jié)
以上是生活随笔為你收集整理的swift学习选pizza项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux C 编程技巧--利用有限状态
- 下一篇: 第一次玩,试试手(标题)