AddressViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // AddressViewController.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 16/10/19.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "AddressViewController.h"
  9. #import "EditAddressCell.h"
  10. #import "MapViewController.h"
  11. @interface AddressViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,pushToMapDelegate,MapChooseAddressControllerDelegate>
  12. @property (nonatomic, strong) UITableView *myTableView;
  13. @property (nonatomic, strong) EditAddressCell *cell;
  14. @property (nonatomic, assign) BOOL isFirst; //是否第一次加载
  15. @property (nonatomic, copy) NSString *keyWordStr;
  16. @end
  17. @implementation AddressViewController
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. _isFirst = YES;
  22. if (!self.provinceStr.length)
  23. {
  24. self.provinceStr = self.BuguLive.province;
  25. }
  26. if (!self.cityStr.length)
  27. {
  28. self.cityStr = self.BuguLive.locationCity;
  29. }
  30. if (!self.areaStr.length)
  31. {
  32. self.areaStr = self.BuguLive.area;
  33. }
  34. [self creatView];
  35. }
  36. - (void)viewWillAppear:(BOOL)animated
  37. {
  38. [super viewWillAppear:animated];
  39. [IQKeyboardManager sharedManager].enable = YES;
  40. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  41. [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  42. [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 5.0f;
  43. if (!_isFirst)
  44. {
  45. [_myTableView reloadData];
  46. }
  47. _isFirst = NO;
  48. }
  49. - (void)viewWillDisappear:(BOOL)animated
  50. {
  51. [super viewWillDisappear:animated];
  52. [IQKeyboardManager sharedManager].enable = NO;
  53. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  54. }
  55. - (void)creatView{
  56. self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithTarget:self action:@selector(backClick) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  57. UIButton *savebutton = [UIButton buttonWithType:UIButtonTypeCustom];
  58. savebutton.frame = CGRectMake(kScreenW-40, 5, 40, 40);
  59. [savebutton setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  60. savebutton.userInteractionEnabled = YES;
  61. savebutton.titleLabel.font = [UIFont systemFontOfSize:15];
  62. [savebutton addTarget:self action:@selector(saveEditButton:) forControlEvents:UIControlEventTouchUpInside];
  63. UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:savebutton];
  64. self.navigationItem.rightBarButtonItem = rightBarButtonItem;
  65. if (self.type == 0)
  66. {
  67. self.title = ASLocalizedString(@"新增地址");
  68. [savebutton setTitle:ASLocalizedString(@"保存")forState:UIControlStateNormal];
  69. }else if (self.type == 1)
  70. {
  71. self.title = ASLocalizedString(@"修改地址");
  72. [savebutton setTitle:ASLocalizedString(@"修改")forState:UIControlStateNormal];
  73. }
  74. self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-kStatusBarHeight-kNavigationBarHeight)];
  75. [self.myTableView registerNib:[UINib nibWithNibName:@"EditAddressCell" bundle:nil] forCellReuseIdentifier:@"EditAddressCell"];
  76. self.myTableView.backgroundColor = kBackGroundColor;
  77. self.myTableView.dataSource = self;
  78. self.myTableView.delegate = self;
  79. self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  80. [self.view addSubview:self.myTableView];
  81. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide)];
  82. //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。
  83. tapGestureRecognizer.cancelsTouchesInView = NO;
  84. //将触摸事件添加到当前view
  85. [_myTableView addGestureRecognizer:tapGestureRecognizer];
  86. }
  87. - (void)keyboardHide
  88. {
  89. [_cell.personFiled resignFirstResponder];
  90. [_cell.phoneFiled resignFirstResponder];
  91. [_cell.cityFiled resignFirstResponder];
  92. [_cell.smallAddressFiled resignFirstResponder];
  93. [_cell.defaultFiled resignFirstResponder];
  94. }
  95. #pragma mark -- dataSource/Delegate
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  97. {
  98. return 1;
  99. }
  100. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  101. {
  102. return 1;
  103. }
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. _cell = [tableView dequeueReusableCellWithIdentifier:@"EditAddressCell"];
  107. _cell.phoneFiled.delegate = self;
  108. _cell.personFiled.delegate = self;
  109. _cell.cityFiled.delegate = self;
  110. _cell.cityFiled.enabled = NO;
  111. _cell.smallAddressFiled.delegate = self;
  112. _cell.backgroundColor = kWhiteColor;
  113. _cell.selectionStyle = UITableViewCellSelectionStyleNone;
  114. _cell.delegate = self;
  115. if (self.personName.length)
  116. {
  117. _cell.personFiled.text = self.personName;
  118. }
  119. if (self.personPhone.length)
  120. {
  121. _cell.phoneFiled.text = self.personPhone;
  122. }
  123. if (self.area.length > 0)
  124. {
  125. _cell.cityFiled.text = self.area;
  126. }else
  127. {
  128. if (self.provinceStr || self.cityStr || self.areaStr)
  129. {
  130. _cell.cityFiled.text = [NSString stringWithFormat:@"%@ %@ %@",self.provinceStr,self.cityStr,self.areaStr];
  131. }
  132. }
  133. if (self.detailArea.length)
  134. {
  135. if (self.BuguLive.area.length > 0)
  136. {
  137. if ([self.detailArea hasPrefix:self.BuguLive.area])
  138. {
  139. self.detailArea = [self.detailArea stringByReplacingOccurrencesOfString:self.BuguLive.area withString:@""];
  140. }
  141. }
  142. _cell.smallAddressFiled.text = self.detailArea;
  143. }else
  144. {
  145. if ([self.BuguLive.locateName hasPrefix:self.BuguLive.area])
  146. {
  147. self.BuguLive.locateName = [self.BuguLive.locateName stringByReplacingOccurrencesOfString:self.BuguLive.area withString:@""];
  148. }
  149. _cell.smallAddressFiled.text = self.BuguLive.locateName;
  150. }
  151. return _cell;
  152. }
  153. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. return 260;
  156. }
  157. #pragma mark 后退
  158. - (void)backClick
  159. {
  160. [self keyboardHide];
  161. [self.navigationController popViewControllerAnimated:YES];
  162. }
  163. #pragma mark 保存
  164. - (void)saveEditButton:(UIButton *)btn
  165. {
  166. btn.userInteractionEnabled = NO;
  167. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  168. [parmDict setObject:@"address" forKey:@"ctl"];
  169. if (self.type == 0)
  170. {
  171. [parmDict setObject:@"addaddress" forKey:@"act"];
  172. }else if(self.type == 1)
  173. {
  174. [parmDict setObject:@"editaddress" forKey:@"act"];
  175. }
  176. if (_cell.personFiled.text.length < 1)
  177. {
  178. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入收货人姓名")];
  179. btn.userInteractionEnabled = YES;
  180. return;
  181. }
  182. [parmDict setObject:_cell.personFiled.text forKey:@"consignee"];
  183. if (_cell.phoneFiled.text.length < 1)
  184. {
  185. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入收货手机号")];
  186. btn.userInteractionEnabled = YES;
  187. return;
  188. }
  189. [parmDict setObject:_cell.phoneFiled.text forKey:@"consignee_mobile"];
  190. if (_cell.cityFiled.text.length < 1)
  191. {
  192. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入行政地区信息")];
  193. btn.userInteractionEnabled = YES;
  194. return;
  195. }
  196. [parmDict setObject:[NSString stringWithFormat:@"{\"area\":\"%@\",\"city\":\"%@\",\"province\":\"%@\"}",self.areaStr,self.cityStr,self.provinceStr] forKey:@"consignee_district"];
  197. if (_cell.smallAddressFiled.text.length < 1)
  198. {
  199. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入详细地址")];
  200. btn.userInteractionEnabled = YES;
  201. return;
  202. }
  203. [parmDict setObject:_cell.smallAddressFiled.text forKey:@"consignee_address"];
  204. if (self.type == 0)
  205. {
  206. [parmDict setObject:@"0" forKey:@"id"];
  207. }else
  208. {
  209. [parmDict setObject:self.address_id forKey:@"id"];
  210. }
  211. [parmDict setObject:@"1" forKey:@"is_default"];
  212. [self keyboardHide];
  213. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  214. {
  215. btn.userInteractionEnabled = YES;
  216. if ([responseJson toInt:@"status"] == 1)
  217. {
  218. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"收货地址保存成功")];
  219. [self.navigationController popViewControllerAnimated:YES];
  220. }else
  221. {
  222. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"收货地址保存不成功")];
  223. }
  224. } FailureBlock:^(NSError *error)
  225. {
  226. btn.userInteractionEnabled = YES;
  227. }];
  228. }
  229. - (void)pushToMapController
  230. {
  231. //腾讯地图
  232. MapViewController *mapVc = [[MapViewController alloc]init];
  233. mapVc.hidesBottomBarWhenPushed = YES;
  234. mapVc.fromType = 0;
  235. mapVc.delegate = self;
  236. [self.navigationController pushViewController:mapVc animated:YES];
  237. }
  238. - (void)chooseAddress:(CLLocationCoordinate2D)location address:(NSString *)address andProvinceString:(NSString *)provinceString andCityString:(NSString *)cityString andAreaString:(NSString *)areaString{
  239. self.provinceStr = provinceString;
  240. self.cityStr = cityString;
  241. self.areaStr = areaString;
  242. if ([provinceString isEqualToString:cityString])
  243. {
  244. self.area = [NSString stringWithFormat:@"%@ %@",provinceString,areaString];
  245. self.detailArea = [address stringByReplacingOccurrencesOfString:provinceString withString:@""];
  246. self.detailArea = [self.detailArea stringByReplacingOccurrencesOfString:areaString withString:@""];
  247. }else
  248. {
  249. self.area = [NSString stringWithFormat:@"%@ %@ %@",provinceString,cityString,areaString];
  250. self.detailArea = [address stringByReplacingOccurrencesOfString:provinceString withString:@""];
  251. self.detailArea = [self.detailArea stringByReplacingOccurrencesOfString:cityString withString:@""];
  252. self.detailArea = [self.detailArea stringByReplacingOccurrencesOfString:areaString withString:@""];
  253. }
  254. [_myTableView reloadData];
  255. }
  256. - (void)textFieldDidEndEditing:(UITextField *)textField
  257. {
  258. if (textField==_cell.phoneFiled)
  259. {
  260. if (_cell.phoneFiled.text.length < 1)
  261. {
  262. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入电话号码")];
  263. return;
  264. }
  265. if (_cell.phoneFiled.text.length < 1)
  266. {
  267. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入11位电话号码")];
  268. return;
  269. }
  270. // if (![_cell.phoneFiled.text isTelephone])
  271. // {
  272. // [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入正确的电话号码")];
  273. // return;
  274. // }
  275. }
  276. }
  277. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  278. {
  279. if (textField == _cell.phoneFiled)
  280. {
  281. if (![string isNumber])
  282. {
  283. return NO;
  284. }
  285. if (string.length == 0) return YES;
  286. NSInteger existedLength = textField.text.length;
  287. NSInteger selectedLength = range.length;
  288. NSInteger replaceLength = string.length;
  289. if (existedLength - selectedLength + replaceLength > 11)
  290. {
  291. return NO;
  292. }
  293. }
  294. return YES;
  295. }
  296. @end