使用通知选择城市
2016-05-18 10:31
375 查看
选择当前城市,在Main.storyboard中实现如图所示:
SelectCityViewController.h中代码如下:
#import <UIKit/UIKit.h>
#define kDisplayProvince 0
#define kDisplayCity 1
#define kDisplayArea 2
@interface SelectCityViewController :
UIViewController<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)int displayType;
@property(nonatomic,strong)NSArray *provinces;
@property(nonatomic,strong)NSArray *citys;
@property(nonatomic,strong)NSArray *areas;
@property(nonatomic,strong)NSString *selectedProvince;//选中的省
@property(nonatomic,strong)NSString *selectedCity;//选中的市
@property(nonatomic,strong)NSString *selectedArea;//选中的区
@end
SelectCityViewController.m中代码如下:
#import "SelectCityViewController.h"
#import "ViewController.h"
@interface
SelectCityViewController ()
@property(nonatomic,strong)NSIndexPath *selectedIndexPath;//当前选中的NSIndexPath
@end
@implementation SelectCityViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
[self
configureData];
[self
configureViews];
}
-(void)configureData{
if (self.displayType
== kDisplayProvince) {
//从文件读取地址字典
NSString *addressPath = [[NSBundle
mainBundle] pathForResource:@"address"
ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary
alloc]initWithContentsOfFile:addressPath];
self.provinces = [dict
objectForKey:@"address"];
}
}
-(void)configureViews{
if (self.displayType
== kDisplayProvince) { //只在选择省份页面显示取消按钮
self.navigationItem.leftBarButtonItem
= [[UIBarButtonItem
alloc]initWithTitle:@"取消"
style:UIBarButtonItemStylePlain
target:self
action:@selector(cancel)];
}
if (self.displayType
== kDisplayArea) {//只在选择区域页面显示确定按钮
self.navigationItem.rightBarButtonItem
= [[UIBarButtonItem
alloc]initWithTitle:@"确定"
style:UIBarButtonItemStylePlain
target:self
action:@selector(submit)];
}
CGRect frame = [self.view
bounds];
self.tableView = [[UITableView
alloc]initWithFrame:frame];
self.tableView.delegate =
self;
self.tableView.dataSource =
self;
[self.view
addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.displayType
== kDisplayProvince) {
return self.provinces.count;
}else if (self.displayType ==
kDisplayCity){
return self.citys.count;
}else{
return self.areas.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* ID =
@"cityCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:ID];
if (self.displayType ==
kDisplayArea) {
cell.accessoryType =
UITableViewCellAccessoryNone;
}else{
cell.accessoryType =
UITableViewCellAccessoryDisclosureIndicator;
}
}
if (self.displayType
== kDisplayProvince) {
NSDictionary *province =
self.provinces[indexPath.row];
NSString *provinceName = [province
objectForKey:@"name"];
cell.textLabel.text= provinceName;
}else if (self.displayType ==
kDisplayCity){
NSDictionary *city =
self.citys[indexPath.row];
NSString *cityName = [city
objectForKey:@"name"];
cell.textLabel.text= cityName;
}else{
cell.textLabel.text=
self.areas[indexPath.row];
cell.imageView.image = [UIImage
imageNamed:@"unchecked"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.displayType
== kDisplayProvince) {
NSDictionary *province =
self.provinces[indexPath.row];
NSArray *citys = [province
objectForKey:@"sub"];
self.selectedProvince = [province
objectForKey:@"name"];
//构建下一级视图控制器
SelectCityViewController *cityVC = [[SelectCityViewController
alloc]init];
cityVC.displayType =
kDisplayCity;//显示模式为城市
cityVC.citys = citys;
cityVC.selectedProvince =
self.selectedProvince;
[self.navigationController
pushViewController:cityVC animated:YES];
}else if (self.displayType ==
kDisplayCity){
NSDictionary *city =
self.citys[indexPath.row];
self.selectedCity = [city
objectForKey:@"name"];
NSArray *areas = [city
objectForKey:@"sub"];
//构建下一级视图控制器
SelectCityViewController *areaVC = [[SelectCityViewController
alloc]init];
areaVC.displayType =
kDisplayArea;//显示模式为区域
areaVC.areas = areas;
areaVC.selectedCity =
self.selectedCity;
areaVC.selectedProvince =
self.selectedProvince;
[self.navigationController
pushViewController:areaVC animated:YES];
}
else{
//取消上一次选定状态
UITableViewCell *oldCell = [tableView
cellForRowAtIndexPath:self.selectedIndexPath];
oldCell.imageView.image = [UIImage
imageNamed:@"unchecked2"];
//勾选当前选定状态
UITableViewCell *newCell = [tableView
cellForRowAtIndexPath:indexPath];
newCell.imageView.image = [UIImage
imageNamed:@"checked2"];
//保存
self.selectedArea =
self.areas[indexPath.row];
self.selectedIndexPath = indexPath;
}
}
-(void)submit{
// NSString *msg = [NSString stringWithFormat:@"%@-%@-%@",self.selectedProvince,self.selectedCity,self.selectedArea];
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"选择地址" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil,
nil];
// [alert show];
//发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"MangoIos" object:nil userInfo:@{@"input":self.selectedArea}];
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)cancel{
[self.navigationController
popViewControllerAnimated:YES];
}
@end
在ViewConreoller.m中代码如下:
#import "ViewController.h"
@interface
ViewController ()
@property (weak,
nonatomic) IBOutlet
UILabel *nowCityLable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.注册通知
/*
通知是一对多的关系,发送一个通知,可以有多个观察者同时存在
第一个参数:添加观察者(接收通知的观察者对象一定要比发送通知的对象先创建)
第二个参数:收到通知之后要执行的方法
第三个参数:通知的名字
第四个参数:传递参数,可以是nil
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLable:) name:@"MangoIos" object:nil];
}
- (void)dealloc{
//3.移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)changeLable:(NSNotification *)notification{
self.nowCityLable.text = notification.userInfo[@"input"];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这样就能实现点击选择城市按钮,选择城市之后就可以把值通过通知传到当前城市。
SelectCityViewController.h中代码如下:
#import <UIKit/UIKit.h>
#define kDisplayProvince 0
#define kDisplayCity 1
#define kDisplayArea 2
@interface SelectCityViewController :
UIViewController<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)int displayType;
@property(nonatomic,strong)NSArray *provinces;
@property(nonatomic,strong)NSArray *citys;
@property(nonatomic,strong)NSArray *areas;
@property(nonatomic,strong)NSString *selectedProvince;//选中的省
@property(nonatomic,strong)NSString *selectedCity;//选中的市
@property(nonatomic,strong)NSString *selectedArea;//选中的区
@end
SelectCityViewController.m中代码如下:
#import "SelectCityViewController.h"
#import "ViewController.h"
@interface
SelectCityViewController ()
@property(nonatomic,strong)NSIndexPath *selectedIndexPath;//当前选中的NSIndexPath
@end
@implementation SelectCityViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
[self
configureData];
[self
configureViews];
}
-(void)configureData{
if (self.displayType
== kDisplayProvince) {
//从文件读取地址字典
NSString *addressPath = [[NSBundle
mainBundle] pathForResource:@"address"
ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary
alloc]initWithContentsOfFile:addressPath];
self.provinces = [dict
objectForKey:@"address"];
}
}
-(void)configureViews{
if (self.displayType
== kDisplayProvince) { //只在选择省份页面显示取消按钮
self.navigationItem.leftBarButtonItem
= [[UIBarButtonItem
alloc]initWithTitle:@"取消"
style:UIBarButtonItemStylePlain
target:self
action:@selector(cancel)];
}
if (self.displayType
== kDisplayArea) {//只在选择区域页面显示确定按钮
self.navigationItem.rightBarButtonItem
= [[UIBarButtonItem
alloc]initWithTitle:@"确定"
style:UIBarButtonItemStylePlain
target:self
action:@selector(submit)];
}
CGRect frame = [self.view
bounds];
self.tableView = [[UITableView
alloc]initWithFrame:frame];
self.tableView.delegate =
self;
self.tableView.dataSource =
self;
[self.view
addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.displayType
== kDisplayProvince) {
return self.provinces.count;
}else if (self.displayType ==
kDisplayCity){
return self.citys.count;
}else{
return self.areas.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* ID =
@"cityCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:ID];
if (self.displayType ==
kDisplayArea) {
cell.accessoryType =
UITableViewCellAccessoryNone;
}else{
cell.accessoryType =
UITableViewCellAccessoryDisclosureIndicator;
}
}
if (self.displayType
== kDisplayProvince) {
NSDictionary *province =
self.provinces[indexPath.row];
NSString *provinceName = [province
objectForKey:@"name"];
cell.textLabel.text= provinceName;
}else if (self.displayType ==
kDisplayCity){
NSDictionary *city =
self.citys[indexPath.row];
NSString *cityName = [city
objectForKey:@"name"];
cell.textLabel.text= cityName;
}else{
cell.textLabel.text=
self.areas[indexPath.row];
cell.imageView.image = [UIImage
imageNamed:@"unchecked"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.displayType
== kDisplayProvince) {
NSDictionary *province =
self.provinces[indexPath.row];
NSArray *citys = [province
objectForKey:@"sub"];
self.selectedProvince = [province
objectForKey:@"name"];
//构建下一级视图控制器
SelectCityViewController *cityVC = [[SelectCityViewController
alloc]init];
cityVC.displayType =
kDisplayCity;//显示模式为城市
cityVC.citys = citys;
cityVC.selectedProvince =
self.selectedProvince;
[self.navigationController
pushViewController:cityVC animated:YES];
}else if (self.displayType ==
kDisplayCity){
NSDictionary *city =
self.citys[indexPath.row];
self.selectedCity = [city
objectForKey:@"name"];
NSArray *areas = [city
objectForKey:@"sub"];
//构建下一级视图控制器
SelectCityViewController *areaVC = [[SelectCityViewController
alloc]init];
areaVC.displayType =
kDisplayArea;//显示模式为区域
areaVC.areas = areas;
areaVC.selectedCity =
self.selectedCity;
areaVC.selectedProvince =
self.selectedProvince;
[self.navigationController
pushViewController:areaVC animated:YES];
}
else{
//取消上一次选定状态
UITableViewCell *oldCell = [tableView
cellForRowAtIndexPath:self.selectedIndexPath];
oldCell.imageView.image = [UIImage
imageNamed:@"unchecked2"];
//勾选当前选定状态
UITableViewCell *newCell = [tableView
cellForRowAtIndexPath:indexPath];
newCell.imageView.image = [UIImage
imageNamed:@"checked2"];
//保存
self.selectedArea =
self.areas[indexPath.row];
self.selectedIndexPath = indexPath;
}
}
-(void)submit{
// NSString *msg = [NSString stringWithFormat:@"%@-%@-%@",self.selectedProvince,self.selectedCity,self.selectedArea];
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"选择地址" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil,
nil];
// [alert show];
//发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"MangoIos" object:nil userInfo:@{@"input":self.selectedArea}];
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)cancel{
[self.navigationController
popViewControllerAnimated:YES];
}
@end
在ViewConreoller.m中代码如下:
#import "ViewController.h"
@interface
ViewController ()
@property (weak,
nonatomic) IBOutlet
UILabel *nowCityLable;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.注册通知
/*
通知是一对多的关系,发送一个通知,可以有多个观察者同时存在
第一个参数:添加观察者(接收通知的观察者对象一定要比发送通知的对象先创建)
第二个参数:收到通知之后要执行的方法
第三个参数:通知的名字
第四个参数:传递参数,可以是nil
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLable:) name:@"MangoIos" object:nil];
}
- (void)dealloc{
//3.移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)changeLable:(NSNotification *)notification{
self.nowCityLable.text = notification.userInfo[@"input"];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这样就能实现点击选择城市按钮,选择城市之后就可以把值通过通知传到当前城市。
相关文章推荐
- [XPath/Python] XPath 与 lxml (二)XPath 语法 - iFantasticMe
- 国与史
- Property和attribute的区别[转]
- Ubuntu 中搭建 LAMP 及 php 开发工具
- Principal Components Regression, Pt.1: The Standard Method
- 【bzoj4591】【Shoi2015超能粒子炮·改】【lucas定理】
- 使用Redis之前5个必须了解的事情
- PKI 1. 加密技术基本原理
- js数据类型,及内存存储学习笔记
- GitHub, Google Code, and other
- java web 监听器listener
- First-欢迎来到IOS世界
- 三、线程的互斥技术问题
- IOS 开发过程中的 消息通知 小红点
- javascript对象属性的赋值解析
- 树、森林和二叉树的转换
- Android Studio的SVN简单使用
- Mac命令行快捷方式
- NotePad++替换行前、行后空格,替换空行
- Nginx反向代理后,图片路径错误