iOS development--uipickerview (selector control) practices for provinces and cities

Source: Internet
Author: User

Uipickerview is a selector control that can generate a single-column selector, or a multicolumn selector

@interface viewcontroller:uiviewcontroller<uipickerviewdatasource,uipickerviewdelegate>

@property (strong,nonatomic) Uipickerview *mypickerview;

@property (strong,nonatomic) Nsmutablearray *provincearr;

@property (strong,nonatomic) Nsmutablearray *cityarr;

@property (strong,nonatomic) Nsarray *arr;

@end

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) viewdidload

{

[Super Viewdidload];

Self. Provincearr=[nsmutablearray array];

Self. Cityarr=[nsmutablearray array];

Path to the file

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

Self.arr=[nsarray Arraywithcontentsoffile:path];

The traversal cycle of the province

for (int i=0;i<self.arr.count; i++)

{

[Self. Provincearr addobject:self.arr[i][@ "state"];

}

Picker control

Self. Mypickerview=[[uipickerview Alloc] Initwithframe:cgrectmake (70, 100, 300, 300)];

Self. mypickerview.delegate=self;

Self. mypickerview.datasource=self;

Self. Mypickerview.backgroundcolor=[uicolor Clearcolor];

[Self.view addsubview:self. Mypickerview];

}

#pragma mark data source Numberofcomponentsinpickerview

-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview

{

return 2;

}

#pragma mark data source Numberofrowsincomponent

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

{

if (component==0)

{

return self. Provincearr.count;

}

Else

{

return self. Cityarr.count;

}

}

#pragma delegate Display information methods

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

{

if (component==0)

{

return self. Provincearr[row];

}

return self. Cityarr[row];

}

#pragma mark Select BOC Information

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

{

if (component==0)

{

[Self. Cityarr Removeallobjects];

Nsinteger Rowprovincearr=[pickerview selectedrowincomponent:0];

Nsarray *arr11=self.arr[rowprovincearr][@ "Cities"];

The traversal cycle in the city

for (int i=0; i<arr11.count; i++)

{

[Self. Cityarr addobject:arr11[i][@ "City"];

}

Refresh Reload

[Self. Mypickerview Reloadcomponent:1];

}

Else

{

Uialertview *myalertview=[[uialertview alloc] initwithtitle:@ "System tip" message:[nsstring stringwithformat:@ "%@--%@ ", [_provincearr objectatindex:row],[_cityarr objectatindex:row]]delegate:nil cancelbuttontitle:@" Cancel " otherbuttontitles:@ "OK", nil];

[Myalertview show];

NSString *str=[nsstring stringwithformat:@ "%@--%@", _provincearr[[pickerview selectedrowincomponent:0]],_cityarr[ Row]];

Prompt box

Uialertcontroller *myalertcontroller=[uialertcontroller alertcontrollerwithtitle:@ "System Tips" message:str Preferredstyle:uialertcontrollerstylealert];

Uialertaction *myalertaction=[uialertaction actionwithtitle:@ "OK" Style:uialertactionstyledefault handler:^ ( Uialertaction * _nonnull Action)

{

NSLog (@ "OK");

}];

Uialertaction *myalertaction1=[uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstyledefault handler:^ ( Uialertaction * _nonnull Action)

{

NSLog (@ "Cancel");

}];

[Myalertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield * _nonnull TextField)

{

Textfield.backgroundcolor=[uicolor Whitecolor];

}];

[Myalertcontroller Addaction:myalertaction1];

[Myalertcontroller addaction:myalertaction];

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

}

}

#pragma mark shows the row height

-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (Nsinteger) component

{

return 50.00;

}

iOS development--uipickerview (selector control) practices for provinces and cities

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.