IOS学习 UIPickerView UItextFiled 省市区 三联动输入

本文介绍了一个iOS应用中实现地址选择功能的方法,包括使用UITableView展示输入字段,并通过UIPickerView进行省市区的选择。代码示例展示了如何加载.plist文件中的地区数据,并将其与选择器联动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "HomeTableViewCell.h"

@interface HomeViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{

    NSArray *nameArray;

    NSArray *messageArray;

    UITableView *tableView;

    

    

    UITableView *addAddressTab;

    UITextField *consignessTextFd;

    UITextField *mobilePhoneTextFd;

    UITextField *selectionTextFd;

    UITextField *detailedDddressTextFd;

}


@property (copy, nonatomic) NSString *state;

@property (copy, nonatomic) NSString *city;

@property (copy, nonatomic) NSString *district;

@property (nonatomic,retain) NSString *selectedState;

@property (nonatomic,retain) NSString *selectedCity;

@property (nonatomic,retain) NSString *selectedArea;


@end





#import "HomeViewController.h"


@interface HomeViewController ()

{

    NSArray *provinces, *cities, *areas;

}

@end


@implementation HomeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

//    

//    nameArray =[[NSArray alloc]initWithObjects:@"收货人:",@"联系手机:",@"地址选择:",@"详细地址:",nil];

//    

//    messageArray = [[NSArray alloc]initWithObjects:@"请输入收货人姓名",@"请输入收货人手机号码",@"请选择地区",@"请输入详细地址",nil];

    

    tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.frame.size.height/3) style:UITableViewStylePlain];

    tableView.delegate = self;

    tableView.dataSource = self;

    [self.view addSubview:tableView];

    

//    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 44*5, self.view.bounds.size.width-100, 44)];

//    [btn setTitle:@"保存地址" forState:UIControlStateNormal];

//    btn.backgroundColor = [UIColor lightGrayColor];

//    btn.layer.cornerRadius = 8;

//    [btn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];

//    [self.view addSubview:btn];

}


-(void)save{

//    NSLog(@"保存");

//    UITextField *textField = (UITextField *)[tableView viewWithTag:102];

//    textField.text = [NSString stringWithFormat:@"%@  %@  %@",_selectedState,_selectedCity,_selectedArea];

//    [textField resignFirstResponder];  //点击返回按钮时,失去第一响应

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    HomeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell==nil) {

        cell=[[HomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    }

    if (indexPath.row ==0) {

        cell.contentLb.text=@"收货人";

        consignessTextFd = [[UITextField alloc]initWithFrame:CGRectMake(130, 8, 120, 30)];

        consignessTextFd.tag =100;

        consignessTextFd.placeholder = @"请输入收货人姓名";

        //显示文本框的光标,要去除文本框背景色

//        consignessTextFd.tintColor=[UIColor whiteColor];        

        consignessTextFd.clearButtonMode = UITextFieldViewModeWhileEditing;//设置清除按钮模式

        [consignessTextFd setBorderStyle:UITextBorderStyleNone]; //外框类型

        consignessTextFd.font=[UIFont fontWithName:@"Times New Roman" size:15];//

        consignessTextFd.clearsOnBeginEditing=YES;//UITextField 的是否出现一件清除按钮

        consignessTextFd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

        [cell addSubview:consignessTextFd];

    }else if (indexPath.row ==1){

        cell.contentLb.text=@"联系手机:";

        mobilePhoneTextFd = [[UITextField alloc]initWithFrame:CGRectMake(130, 8, 120, 30)];

         mobilePhoneTextFd.tag =101;

        mobilePhoneTextFd.placeholder = @"请输入手机号码";

        mobilePhoneTextFd.tintColor=[UIColor whiteColor];

        mobilePhoneTextFd.clearButtonMode = UITextFieldViewModeWhileEditing;

        [mobilePhoneTextFd setBorderStyle:UITextBorderStyleNone]; //外框类型

        mobilePhoneTextFd.font=[UIFont fontWithName:@"Times New Roman" size:15];//

        mobilePhoneTextFd.clearsOnBeginEditing=YES;//UITextField 的是否出现一件清除按钮

        mobilePhoneTextFd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

        [cell addSubview:mobilePhoneTextFd];


    }else if (indexPath.row ==2){

        cell.contentLb.text=@"地址选择:";

        selectionTextFd = [[UITextField alloc]initWithFrame:CGRectMake(130, 8, 120, 30)];

        selectionTextFd.tag =103;

        selectionTextFd.delegate=self;

        selectionTextFd.placeholder = @"请选择地址";

        selectionTextFd.tintColor=[UIColor whiteColor];

        selectionTextFd.clearButtonMode = UITextFieldViewModeWhileEditing;

        [selectionTextFd setBorderStyle:UITextBorderStyleNone]; //外框类型

        selectionTextFd.font=[UIFont fontWithName:@"Times New Roman" size:15];//

        selectionTextFd.clearsOnBeginEditing=YES;//UITextField 的是否出现一件清除按钮

        selectionTextFd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

        [cell addSubview:selectionTextFd];


    }else{

        cell.contentLb.text=@"详细地址:";

        detailedDddressTextFd = [[UITextField alloc]initWithFrame:CGRectMake(130, 8, 120, 30)];

        detailedDddressTextFd.tag =104;

        detailedDddressTextFd.placeholder = @"请输入详细地址";

        detailedDddressTextFd.tintColor=[UIColor whiteColor];

        detailedDddressTextFd.clearButtonMode = UITextFieldViewModeWhileEditing;

        [detailedDddressTextFd setBorderStyle:UITextBorderStyleNone]; //外框类型

        detailedDddressTextFd.font=[UIFont fontWithName:@"Times New Roman" size:15];//

        detailedDddressTextFd.clearsOnBeginEditing=YES;//UITextField 的是否出现一件清除按钮

        detailedDddressTextFd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

        [cell addSubview:detailedDddressTextFd];

    }

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 45;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    

}


- (void)textFieldDidBeginEditing:(UITextField *)textField{

    //宽和高是固定的,:320,216

    if (textField ==selectionTextFd) {

        UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-400, 0, 0)];

        pickerView.delegate = self;

        pickerView.dataSource = self;

        pickerView.showsSelectionIndicator = YES;

        //设置弹出视图(不弹键盘弹图片)

        textField.inputView = pickerView;

        

        //获取plist文件路径,两种方式

        NSBundle *bundle = [NSBundle mainBundle];

        NSString *path = [bundle pathForResource:@"area" ofType:@"plist"];

        //读取文件内容

        provinces = [[NSArray alloc]initWithContentsOfFile:path];

        cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];

        areas = [[cities objectAtIndex:0] objectForKey:@"areas"];

        

        //设置初始值

        self.state = [[provinces objectAtIndex:0] objectForKey:@"state"];  //

        self.city = [[cities objectAtIndex:0] objectForKey:@"city"];  //

        //地区

        if (areas.count > 0) {

            self.district = [areas objectAtIndex:0];

        } else{

            self.district = @"";

        }


    }

    

}


//列数

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

    return 3;

}


//行数

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

    switch (component) {

        case 0:

            return [provinces count];

            break;

        case 1:

            return [cities count];

            break;

            

        default:

            return [areas count];

            break;

    }

}


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

    switch (component) {

        case 0:

            return [[provinces objectAtIndex:row] objectForKey:@"state"];

            break;

        case 1:

            return [[cities objectAtIndex:row] objectForKey:@"city"];

            break;

        case 2:

            if ([areas count] > 0) {

                return [areas objectAtIndex:row];

                break;

            }

        default:

            return  @"";

            break;

    }

}


//选择行事件,监听轮子的移动

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

{

    switch (component) {

        case 0:

            cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];

            //将指定的行滚动到中心

            [pickerView selectRow:0 inComponent:1 animated:YES];

            //刷新指定列中的行

            [pickerView reloadComponent:1];

            

            areas = [[cities objectAtIndex:0] objectForKey:@"areas"];

            [pickerView selectRow:0 inComponent:2 animated:YES];

            [pickerView reloadComponent:2];

            

            self.state = [[provinces objectAtIndex:row] objectForKey:@"state"];

            self.city = [[cities objectAtIndex:0] objectForKey:@"city"];

            if ([areas count] > 0) {

                self.district = [areas objectAtIndex:0];

            } else{

                self.district = @"";

            }

            break;

        case 1:

            areas = [[cities objectAtIndex:row] objectForKey:@"areas"];

            [pickerView selectRow:0 inComponent:2 animated:YES];

            [pickerView reloadComponent:2];

            

            self.city = [[cities objectAtIndex:row] objectForKey:@"city"];

            if ([areas count] > 0) {

                self.district = [areas objectAtIndex:0];

            } else{

                self.district = @"";

            }

            break;

        case 2:

            if ([areas count] > 0) {

                self.district = [areas objectAtIndex:row];

            } else{

                self.district = @"";

            }

            break;

        default:

            break;

    }

    

    NSInteger selectedProvinceIndex = [pickerView selectedRowInComponent:0];

    _selectedState = [[provinces objectAtIndex:selectedProvinceIndex]objectForKey:@"state"];

    

    NSInteger selectedCityIndex = [pickerView selectedRowInComponent:1];

    _selectedCity = [[cities objectAtIndex:selectedCityIndex]objectForKey:@"city"];

    

    if (areas.count > 0) {

        NSInteger selectedAreaIndex = [pickerView selectedRowInComponent:2];

        _selectedArea = [areas objectAtIndex:selectedAreaIndex];

    } else{

        _selectedArea = @"";

    }

    selectionTextFd.text = [NSString stringWithFormat:@"%@  %@  %@",_selectedState,_selectedCity,_selectedArea];

}






#import <UIKit/UIKit.h>

#define HEL_NEUE_REGULAR(FONT_SIZE)         [UIFont fontWithName:@"HelveticaNeue" size:FONT_SIZE]


#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]


@interface HomeTableViewCell : UITableViewCell


@property(retain,nonatomic)UILabel *contentLb;

@end





#import "HomeTableViewCell.h"


@implementation HomeTableViewCell


- (void)awakeFromNib {

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];


    // Configure the view for the selected state

}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)

    {

        [self initUI];

    }

    return self;

}


-(void)initUI{

  

    

    _contentLb =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 21)];

    _contentLb.font = HEL_NEUE_REGULAR(15);

    _contentLb.textColor =UIColorFromRGB(000000);

    [self.contentView addSubview:_contentLb];

}

@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值