iOS-0.1-UIPickView

Outline

`细节描述

`UIPickerView



`细节描述

>>属性

swift可以设置计算属性和属性监视器

willSet与didSet设置属性监视器

//带属性监视器的普通属性
var age:Int = 0
{
    //我们需要在age属性变化前做点什么
    willSet
    {
        println("Will set an new value \(newValue) to age")
    }
    //我们需要在age属性发生变化后,更新一下nickName这个属性
    didSet
    {
        println("age filed changed form \(oldValue) to \(age)")
        if age<10
        {
            nickName = "Little"
        }else
        {
            nickName = "Big"
        }
    }
}

>>枚举与switch语句

enum 枚举类型

enum Feat:Int{
    case fit = 0, ni,i
}
//可能返回为nil
let u = Feat(rawValue: 4)

>>问号

可选型的定义

tmp:Class?

>>异常处理

// 方法一:推荐 try? 如果解析成功就有值,否则为 nil问号表示返回可能是nil值

let json = try? JSONSerialization.jsonObject(with: data!, options: []);  

// 方法二:不推荐 try! 如果解析成功就有值,否则崩溃

let json2 = try! JSONSerialization.jsonObject(with: data!, options: []);  

// 方法三:处理异常,能够接收到错误,并且输出。但是,语法结构复杂。

do {  
    let json3 = try JSONSerialization.jsonObject(with: data!, options: []);  
    print(json3);  
} catch {  
    print(error);  
}  

>>函数

可以通过参数列表的不同重载函数



`UIPickerView

控件长这样,用来选择,另外选择日期的叫UIDatePicker

主要实现两个protocol:UIPickerViewDelegate, UIPickerViewDataSource。承担了提供显示内容的功能

delegate相当于申请回调函数,可以承担类似事件响应的功能

import UIKit


/*
 Represents the different features used by this model.
 */
enum Feature: Int {
    case first = 0, second, third
}



class MyTableViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {
    @IBOutlet weak var l1: UILabel!
    @IBOutlet weak var l3: UILabel!
    @IBOutlet weak var l2: UILabel!
    @IBOutlet weak var picker: UIPickerView!
    {
//属性监视器
        didSet{
            picker.dataSource = self
            picker.delegate = self
        }
    }
    // UIPickerViewDataSource  returns the number of 'columns' to display.
    public func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 3
    }
    
    // UIPickerViewDataSource  returns the # of rows in each component..
    public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        guard let col:Feature = Feature(rawValue: component) else{
            fatalError("wrong number")
        }
        switch col
        {
            case .first:
                return 2
            case .second:
                return 3
            case .third:
                return 4
        }
    }
    
    
    
    let data1:[String] = ["12","123"]
    let data2:[String] = ["212","2123","2wode"]
    let data3:[String] = ["312","3123","3wode","fffsdd"]
    // UIPickerViewDelegate
    // these methods return either a plain NSString, a NSAttributedString, or a view (e.g UILabel) to display the row for the component.
    // for the view versions, we cache any hidden and thus unused views and pass them back for reuse.
    // If you return back a different object, the old one will be released. the view will be centered in the row rect
    public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
        guard let numberCol = Feature(rawValue: component) else
        {
            return nil
        }
        switch numberCol{
        case .first:
            return data1[row]
        case .second:
            return data2[row]
        case .third:
            return data3[row]
        }
        
    }
    
    // 事件响应的工作 交由 delegate机制实现
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //
        l1.text = String(data1[pickerView.selectedRow(inComponent: Feature.first.rawValue)])
        l2.text = String(data2[pickerView.selectedRow(inComponent: Feature.second.rawValue)])
        l3.text = String(data3[pickerView.selectedRow(inComponent: Feature.third.rawValue)])
    }
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值