日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用UIpickView实现省市的联动

發布時間:2023/12/9 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用UIpickView实现省市的联动 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>

@property(strong,nonatomic)UIPickerView *pickView;

//定義一個可變數組用于存放省的數據

@property(strong,nonatomic)NSMutableArray *Statearry;

//定義一個可變數組用于存放市的數據

@property(strong,nonatomic)NSMutableArray *Citiesarry;

//定義一個集合分別存省和市的數據

@property(strong,nonatomic)NSArray *arry;

@end

?

#import "ViewController.h"

?

@interface ViewController ()

?

@end

?

@implementation ViewController

?

- (void)viewDidLoad {

? ? [super viewDidLoad];

?? ?

? ? //獲取數據

? ? NSString *path=[[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];? ??

? ? //初始化省和市的數組

? ? self.Statearry=[NSMutableArray array];

? ? self.Citiesarry=[NSMutableArray array];

? ? //ayyr這個大數組存放所有的省市

? ? self.arry=[NSArray arrayWithContentsOfFile:path];

?

? ? //獲取省份的,將取出來的省份數據放在省的可變集合Statearry里

? ? for (NSDictionary *arr in self.arry)

? ? {

? ? ? ? [self.Statearry addObject:arr[@"State"]];

? ? }

?? ?

?? ?

? ? //創建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 數據源 Method numberOfComponentsInPickerView

?- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

? ? //兩列

? ? return 2;

}

?

#pragma mark 數據源 Method pickerViewOfRows

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

? ? if (component==0)

? ? {

? ? ? ? //省份的個數

? ? ? ? return self.Statearry.count;

? ? }

? ? else

? ? {

? ? ? ? //市的個數

? ? ? ? return self.Citiesarry.count;

? ? }

}

?#pragma mark delegate 顯示信息的方法

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

? ? if (component==0)

? ? {

? ? ? ? //選擇的省份

? ? ? ? return self.Statearry[row];

? ? }

? ? else

? ? {

? ? ? ? //選擇的市

? ? ? ? return self.Citiesarry[row];

? ? }??

}

#pragma mark 選中行的信息

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

? ? if (component==0)

? ? {

?? ? ? ?

? ? ? ? //清空上一次市的那一列留下來的數據

? ? ? ? [self.Citiesarry removeAllObjects];

? ? ? ? //定義一個index,找出第一個滾動條里面的所有省對應的下標找出來,賦值給index

? ? ? ? NSInteger index=[pickerView selectedRowInComponent:0];

? ? ? ? //遍歷出所有市

? ? ? ? for (NSDictionary *city in self.arry[index][@"Cities"])

? ? ? ? {

? ? ? ? ? ? //將遍歷出來市追加到存放市的集合里

? ? ? ? ? ? [self.Citiesarry addObject:city[@"city"]];

? ? ? ? }

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

?? ?

? ? ? ? //更新第二個滾輪的數據

? ? ? ? [self.pickView reloadComponent:1];

? ? }

? ? else

? ? {

? ? ? ? //顯示取出來的省和市

? ? ? ? NSString *message=[NSString stringWithFormat:@"你選擇的是%@的%@?",self.Statearry[[pickerView selectedRowInComponent:0]],self.Citiesarry[row]];

?? ? ? ?

? ? ? ? //設置彈出框的標題

? ? ? ? UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];

?? ? ? ?

? ? ? ? //設置按鈕名稱

? ? ? ? UIAlertAction *okAction=[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

?? ? ? ? ? ?

? ? ? ? }];

? ? ? ? //設置按鈕名稱

? ? ? ? UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

? ? ? ? //將按鈕加到提示框里面

? ? ? ? [alert addAction:okAction];

? ? ? ? [alert addAction:cancelAction];

? ? ? ? //

? ? ? ? [self presentViewController:alert animated:YES completion:nil];

? ? }? ? ? ?

}

- (void)didReceiveMemoryWarning {

? ? [super didReceiveMemoryWarning];

? ? // Dispose of any resources that can be recreated.

}

@end

?

轉載于:https://www.cnblogs.com/layios/p/5270277.html

總結

以上是生活随笔為你收集整理的用UIpickView实现省市的联动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。