On the introduction of Swift development: selector (Uipickerview)

Source: Internet
Author: User

reprint Please declare source: http://blog.csdn.net/jinnchang/article/details/44487269
------------------------------------------------------------------------------------------
1, Summary
------------------------------------------------------------------------------------------
2. Code
viewcontroller.swift//uipickerviewsample////Created by Jinnchang on 15/3/18.//Copyright (c) 2015 Jinn Chang. All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller, Uipickerviewdelegate,        Uipickerviewdatasource {var mypickerview:uipickerview?        var provinces = [string: [string]] () var cities = [String] () override Func Viewdidload () {//Load data Provinces = ["Zhejiang Province": ["Hangzhou City", "Ningbo"], "Anhui Province": ["Huangshan", "Hefei"]] cities = provinces.values.array[0]//define a button, Make the data return to the default state let Button1 = Uibutton.buttonwithtype (. System) as? UIButton button1? frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-$, +,-) button1? Settitle ("Back to Default state", ForState:UIControlState.Normal) button1?. AddTarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside) button1?. Tag = 1//define a button to refresh all data let Button2 = Uibutton.buttonwithtype (. System) as? UIButton Button2?. frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-$, button2,.) Settitle ("Refresh all elements", forState:UIControlState.Normal) button2?. AddTarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside) button2?. Tag = 2//Define a button that displays the currently selected provinces and cities let Button3 = Uibutton.buttonwithtype (. System) as? UIButton Button3? frame = CGRectMake (SELF.VIEW.FRAME.WIDTH/2-$, +,--) Button3? Settitle ("Show currently selected provinces and cities", ForState:UIControlState.Normal) Button3?. AddTarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.TouchUpInside) Button3?. Tag = 3//Initialize Mypickerview Mypickerview = Uipickerview (Frame:cgrectmake (0, Self.view.frame.height -Mypickerview, Self.view.frame.width, ())? delegate = Self Mypickerview?. DataSource = self//show check box, IOS7 will not work after Mypickerview?  Showsselectionindicator = False Self.view.addSubview (button1!)      Self.view.addSubview (button2!)        Self.view.addSubview (button3!)    Self.view.addSubview (mypickerview!)        }//Set number of columns Func Numberofcomponentsinpickerview (pickerview:uipickerview), Int {return 2} Set number of rows func pickerview (Pickerview:uipickerview, Numberofrowsincomponent component:int), Int {if (compone NT = = 0) {return provinces.keys.array.count} if (component = = 1) {return Provinces.cou NT} return 0}//Set the specific contents of each line (Titleforrow and Viewforrow both implement one) Func Pickerview (Pickerview:uipi Ckerview, Titleforrow row:int, Forcomponent component:int), string! {if (component = = 0) {return Provinces.keys.array[row]} if (component = = 1) {RE Turn Cities[row]} return nil}//The operation of the selected row func Pickerview (Pickerview:uipickerview, Didselectro    W row:int, Incomponent component:int) {if (component = = 0) {        Cities = provinces[provinces.keys.array[row]]! Reload level two option and reset Mypickerview?. Reloadcomponent (1) mypickerview?        SelectRow (0, Incomponent:1, animated:true)}}///Response button click event func buttonaction (Sender:uibutton) { Let num = sender.tag switch num {Case 1://restore data and reset provinces = ["Zhejiang Province": ["Hangzhou City", "Ning Wave City "]," Anhui Province ": [" Huangshan "," Hefei "]] cities = provinces.values.array[0] Mypickerview?. Reloadallcomponents () Mypickerview? SelectRow (0, incomponent:0, animated:true) Mypickerview? SelectRow (0, Incomponent:1, animated:true) Case 2://Load new data and reset provinces = ["Zhejiang": ["Hangzhou", "Ningbo" ], "Anhui": ["Huangshan", "Hefei"]] cities = provinces.values.array[0] Mypickerview? Reloadallcomponents () Mypickerview? SelectRow (0, incomponent:0, animated:true) Mypickerview?   SelectRow (0, Incomponent:1, animated:true) Case 3:         Displays the currently selected provinces and cities let Provincenum = Mypickerview?. Selectedrowincomponent (0) Let Citynum = Mypickerview?. Selectedrowincomponent (1) println ("Province:\ (provinces.keys.array[provincenum!)");        City:\ (cities[citynum!]) Default:break}}}
------------------------------------------------------------------------------------------
3, ResourceProject on Github address: uipickerviewsample

Article last updated:. The resources are as follows:

Uipickerview Class Reference

UIKit User Interface catalog:picker views

On the introduction of Swift development: selector (Uipickerview)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.