Ios.uikit.11.uipickerview

Source: Internet
Author: User

First, Case introduction: Click UIButton, the selected provincial and municipal information displayed in the Uilabel, 01,02

Fig. 01 FIG. 02

Second, the case steps:

1, choose Simple View aplication, named cq.37. General Selector, 03

Figure 03

2, main.storyboard,04

3, Provinces_cities.plist

4, CQ37ViewController.h

#import <UIKit/UIKit.h>@interface cq37viewcontroller:uiviewcontroller< uipickerviewdatasource,uipickerviewdelegate>** ** *  Pickercitiesdata; -(Ibaction) onclick: (ID) sender; @end

5, CQ37VIEWCONTROLLER.M

#import "CQ37ViewController.h"@interfaceCq37viewcontroller ()@end@implementationCq37viewcontroller- (void) viewdidload{[Super Viewdidload]; NSBundle*mainbundle =[NSBundle Mainbundle]; NSString*plistpath = [Mainbundle pathforresource:@"provinces_cities"OfType:@"plist"]; Nsdictionary*dict =[[Nsdictionary alloc] initwithcontentsoffile:plistpath]; Self.pickerdata=dict; Self.pickerprovincesdata=[Dict AllKeys]; NSString*selectedprovince = [Self.pickerprovincesdata objectatindex:0]; Self.pickercitiesdata=[Self.pickerdata objectforkey:selectedprovince]; Self.pickerview.Delegate=Self ; Self.pickerView.dataSource=Self ;}-(Ibaction) onclick: (ID) sender{Nsinteger row1= [Self.pickerview selectedrowincomponent:0]; Nsinteger Row2= [Self.pickerview selectedrowincomponent:1]; NSString*selected1 =[Self.pickerprovincesdata Objectatindex:row1]; NSString*selected2 =[Self.pickercitiesdata Objectatindex:row2]; NSString*title = [[NSString alloc] Initwithformat:@"%@,%@", Selected1,selected2]; Self.label.text=title;}#pragmaMark Uipickerviewdatasource method-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{return 2;}-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{if(Component = =0) {        return[Self.pickerprovincesdata Count]; }Else{        return[Self.pickercitiesdata Count]; }}#pragmaMark implements the protocol Uipickerviewdelegate method-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component {if(Component = =0) {//Select Province Name        return[Self.pickerprovincesdata Objectatindex:row]; } Else{//Select city Name        return[Self.pickercitiesdata Objectatindex:row]; }}- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component {if(Component = =0) {NSString*seletedprovince =[Self.pickerprovincesdata Objectatindex:row]; Nsarray*array =[Self.pickerdata objectforkey:seletedprovince]; Self.pickercitiesdata=Array; [Self.pickerview reloadcomponent:1]; }}@end

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.