您的位置:首页 > 移动开发 > IOS开发

IOS处女做--天气预报

2016-01-31 11:48 405 查看
内容:

先贴几张效果图吧









 


1、使用坐标反编译出地址
开始定位
[self.locationManger startUpdatingLocation];
定位服务管理初始化
self.locationManger = [[CLLocationManager alloc] init];
self.locationManger.delegate = self;
self.locationManger.desiredAccuracy = kCLLocationAccuracyBest;//据说比较耗电额
self.locationManger.distanceFilter = 1000.0f;//1000m
定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"%@",error);
[self showAlert];    //弹出警告框!
}
定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *currLocation = [locations lastObject];
[self reverseGeocode:currLocation];   //地址反编译!
}
地址反编译
- (void)reverseGeocode:(CLLocation *) currLocation{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray *placemarks,NSError *error){
if ([placemarks count]>0) {
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary = placemark.addressDictionary;
NSString *city = [addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey];
//为了方便我分别提取了“市”,“省”之前的字
NSString *state = [addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey];
NSRange range = [state rangeOfString:@"省"];
state = [state substringToIndex:range.location];
_nowProvince = state;
self.labelProvince.text = _nowProvince;
range = [city rangeOfString:@"市"];
city = [city substringToIndex:range.location];
_nowCity = city;
self.labelCity.text = _nowCity;
}
}];
}


2、我使用的一些控件
在这个app中我用到了imageview,button,label等
总结一下tableview的使用


一开始准备使用自定义的单元格,最后想想没有必要就没写

设置表行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listData count];
}

设置单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSInteger row = [indexPath row];
cell.textLabel.text = [[self.listData objectAtIndex:row]objectForKey:@"city"];
return cell;
}
注意:将cell的Accessory设置为Discolure XXX
选择表示图行时触发
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"SendData"]) {
ViewController *receive = segue.destinationViewController;
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow]row];
NSString *selectedName = [[self.listData objectAtIndex:selectedIndex]objectForKey:@"city"];
receive.nowProvince = self.province;
receive.nowCity = selectedName;
}
}


3、在两个ViewController之间传值
使用model的方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"ForeInfo"]) {
DetailViewController *receive;
receive = segue.destinationViewController;
receive.listData = [self.listData objectForKey:@"forecast"];
}
}


4、在屏幕适配的方面,我主要使用pin



5、文件的读取

我将省份,城市存在文件中(网上下的,要的话可以在下面回复额)
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"ProvincesAndCities" ofType:@"plist"];
self.arrayData = [[NSArray alloc] initWithContentsOfFile:path];


6、使用的API
 http://apis.baidu.com/apistore/weatherservice/citylist

具体使用的代码度娘有(灰常的方便)!!

把城市名转化为UTF8
NSURL *url = [NSURL URLWithString:[_nowCity stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
httpArg = [[NSString alloc] initWithFormat:@"cityname=%@",url];</strong>


总结:

由于第一次制作app存在着许多的不足,比如:数据的持久化这一方面,使用segue传值虽然方便但也有一些缺陷,随着水平的上升我也会去把这个软件做的完善

。在做app的同时我感觉对objective-C的掌握以及对ios平台也有了一定的理解,和光观看书相比,实践提升更快更有效!!
最后还是感觉Google挺好用哒
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios