Using Uipickview to realize the linkage between provinces and cities

Source: Internet
Author: User

#import <UIKit/UIKit.h>

@interface viewcontroller:uiviewcontroller<uipickerviewdatasource,uipickerviewdelegate>

@property (strong,nonatomic) Uipickerview *pickview;

Define a mutable array to hold the data in the province

@property (strong,nonatomic) Nsmutablearray *statearry;

Defines a variable array for storing the data in the city

@property (strong,nonatomic) Nsmutablearray *citiesarry;

Define a collection of data for each province and city

@property (strong,nonatomic) Nsarray *arry;

@end

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Get Data

NSString *path=[[nsbundle Mainbundle] pathforresource:@ "City" oftype:@ "plist"];

Initializing an array of provinces and cities

Self. Statearry=[nsmutablearray array];

Self. Citiesarry=[nsmutablearray array];

Ayyr this large array to store all the provinces and cities

Self.arry=[nsarray Arraywithcontentsoffile:path];

Get the provinces, and put the data in the province's variable set Statearry

For (Nsdictionary *arr in Self.arry)

{

[Self. Statearry addobject:arr[@ "state"];

}

Create Pickview

Self.pickview=[[uipickerview Alloc] Initwithframe:cgrectmake (0, 200, 414, 200)];

Self.pickview.backgroundcolor=[uicolor Graycolor];

self.pickview.delegate=self;

self.pickview.datasource=self;

[Self.view AddSubview:self.pickView];

}

#pragma mark data source Method Numberofcomponentsinpickerview

-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview

{

Two columns

return 2;

}

#pragma mark data source Method pickerviewofrows

-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component

{

if (component==0)

{

Number of Provinces

return self. Statearry.count;

}

Else

{

Number of cities

return self. Citiesarry.count;

}

}

#pragma mark delegate how to display information

-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component

{

if (component==0)

{

Selected provinces

return self. Statearry[row];

}

Else

{

City of Choice

return self. Citiesarry[row];

}

}

#pragma Mark selects a line of information

-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component

{

if (component==0)

{

Clear the data from the last City column.

[Self. Citiesarry Removeallobjects];

Define an index to find out the corresponding subscript for all the provinces in the first scroll bar and assign the value to index

Nsinteger Index=[pickerview selectedrowincomponent:0];

Traverse out all the city

For (Nsdictionary *city in self.arry[index][@ "Cities"])

{

Append the walk-out city to the collection in the storage city

[Self. Citiesarry addobject:city[@ "City"];

}

NSLog (@ "%@", self. Citiesarry);

Update the data for the second wheel

[Self.pickview reloadcomponent:1];

}

Else

{

Show the provinces and municipalities taken out

NSString *message=[nsstring stringwithformat:@ "You chose%@ 's%@?", self. Statearry[[pickerview selectedrowincomponent:0]],self. Citiesarry[row]];

Set the caption of a popup box

Uialertcontroller *alert=[uialertcontroller alertcontrollerwithtitle:@ "hint" message:message PreferredStyle: Uialertcontrollerstylealert];

Set button name

Uialertaction *okaction=[uialertaction actionwithtitle:@ "OK" Style:uialertactionstyledefault handler:^ ( Uialertaction *action) {

}];

Set button name

Uialertaction *cancelaction=[uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel Handler:nil];

Add a button to the Hint box

[Alert addaction:okaction];

[Alert addaction:cancelaction];

//

[Self Presentviewcontroller:alert animated:yes completion:nil];

}

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

Using Uipickview to realize the linkage between provinces and cities

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.