IOS开发基础知识--碎片6
2014-12-25 10:07
351 查看
三十三:IOS多视图跳转方法
三十四:presentModalViewController与dismissModalViewControllerAnimated注意事项
三十五:视图跳载的几种动画
UIModalTransitionStyleCoverVertical//新视图从下向上出现
UIModalTransitionStyleFlipHorizontal//以设备的长轴为中心翻转出现
UIModalTransitionStyleCrossDissolve//渐渐显示
UIModalTransitionStylePartialCurl//原视图向上卷起
三十六:JSONKit的使用方法
三十七:三十五:afnetworking2.0运用,结合json,引入afnetworking文件后,引入头文件就可以使用,支持arc
第一种: 跳转:[selfpresentModalViewController:controlanimated:YES]; 返回:[selfdismissModalViewControllerAnimated:YES]; 第二种: 跳转:[self.navigationControllerpushViewController:subTableViewControlleranimated:YES]; 返回:[self.navigationControllerpopViewControllerAnimated:YES]; 第三种:自己控制:[self.viewaddSubview:<#(UIView*)#>][self.viewremoveFromSuperview]; 注意:pushViewController和popViewController进行视图间的切换,就必须要求当前视图是个NavigationController, 第四种: //根据SegueID执行跳转 [selfperformSegueWithIdentifier:@"contactList"sender:nil];
三十四:presentModalViewController与dismissModalViewControllerAnimated注意事项
在实际开发中,如果要弹出视图: 我们常用到presentModalViewController方法和dismissModalViewControllerAnimated方法。 presentModalViewController:弹出视图 dismissModalViewControllerAnimated:隐藏视图 弹出视图: FeedbackViewController*feedbackViewController=[[FeedbackViewControlleralloc]initWithNibName:@"FeedbackViewController"bundle:nil]; UINavigationController*navigationController=[[UINavigationControlleralloc]initWithRootViewController:feedbackViewController]; [selfpresentModalViewController:navigationControlleranimated:YES]; 隐藏视图: [selfdismissModalViewControllerAnimated:YES]; 关于这两个方法的几点说明: 1.iPhone上弹出/隐藏视图时,使用为全屏模式 2.搞清楚谁是presenting,谁是presented 如果A弹出B,那么A为presenting,B为presented。 3.隐藏视图的策略 我们假如A弹出B 就是说,A负责隐藏B;如果我们在B中调用dismissModalViewControllerAnimated方法,那么编译器,自动将消息发送给A。 等等,什么消息? 简单的理解,当执行presentModalViewController:方法:在A弹出B时: 执行A的viewWillDisappear方法, 通知B执行自己的viewWillAppear方法和viewDidAppear 执行A的viewDidDisappear方法 当执行dismissModalViewControllerAnimated方法:隐藏B时: 执行B的viewWillDisappear 通知A执行自己的viewWillAppear方法和viewDidAppear方法 执行B的viewDidDisappear方法 以下我做了个测试来输出一轮AB切换: A:More B:Feed 2012-12-2714:01:23.666WTV[1627:11303]-More--viewWillDisappear---- 2012-12-2714:01:23.672WTV[1627:11303]-Feed--viewWillAppear---- 2012-12-2714:01:24.086WTV[1627:11303]-Feed--viewDidAppear---- 2012-12-2714:01:24.087WTV[1627:11303]-More--viewDidDisappear---- 2012-12-2714:01:25.745WTV[1627:11303]-Feed--viewWillDisappear---- 2012-12-2714:01:25.745WTV[1627:11303]-More--viewWillAppear---- 2012-12-2714:01:26.156WTV[1627:11303]-More--viewDidAppear---- 2012-12-2714:01:26.157WTV[1627:11303]-Feed--viewDidDisappear---- 当我们信心慢慢,庆幸我们可以了解了这两个方法时,悲剧发生了: 4.苹果官方已经把这两个方法DeprecatediniOS6.0.了 -(void)presentModalViewController:(UIViewController*)modalViewControlleranimated:(BOOL)animated; -(void)dismissModalViewControllerAnimated:(BOOL)animated; 取而代之的是: [selfpresentViewController:navigationController animated:YES completion:^(void){ //Code }]; [selfdismissViewControllerAnimated:YES completion:^(void){ //Code }]; 新接口的差别是提供了一个参数,允许你传入一个block。这个block的回调方法在VC的viewWillDisappear方法后调用。也就是被隐藏的VC对象被释放后运行回调。 这样做的好处:可以方便做多个UI效果之间的衔接和转换。
三十五:视图跳载的几种动画
BaiDuViewController*baiduController=[mainStoryboardinstantiateViewControllerWithIdentifier:@"baiduStoryboard"]; baiduController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal; [selfpresentViewController:baiduControlleranimated:YEScompletion:^{ }];
UIModalTransitionStyleCoverVertical//新视图从下向上出现
UIModalTransitionStyleFlipHorizontal//以设备的长轴为中心翻转出现
UIModalTransitionStyleCrossDissolve//渐渐显示
UIModalTransitionStylePartialCurl//原视图向上卷起
三十六:JSONKit的使用方法
json开源的类库有很多,其中JSONKit库是非常简单易用而且效率又比较高的,重要的JSONKit适用于ios5.0以下的版本。
下载地址:https://github.com/johnezang/JSONKit
使用JSONKit库来解析json文件,只需要下载JSONKit.h和JSONKit.m添加到工程中,设置支持arc,在项目中buildphases中的compilesources,选择jsonkit.m然后确认键输入-fno-objc-arc
#import"JSONKit.h"
//假设strJson是网络上接收到的json字符串,
NSString*strJson=@"[{\"Id\":1,\"BrandName\":\"爱马仕\"},{\"Id\":2,\"BrandName\":\"安娜苏\"}]";
NSArray*arrlist=[strJsonobjectFromJSONString];
NSLog(@"%d",[arrlistcount]);
for(inti=0;i<[arrlistcount];i++){
NSDictionary*item=[arrlistobjectAtIndex:i];
NSString*BrandName=[itemobjectForKey:@"BrandName"];
NSLog(@"%@",BrandName);
}
字典arrlist便是解析好的json文件了。
JSONKit库也可以用来生成json文件
NSMutableDictionary*jsonDic=[NSMutableDictionarydictionary];
NSMutableDictionary*alert=[NSMutableDictionarydictionary]
;NSMutableDictionary*aps=[NSMutableDictionarydictionary];
[alertsetObject:@"amsgcome!"forKey:@"body"];
[apssetObject:alertforKey:@"alert"];
[apssetObject:@"3"forKey:@"bage"];
[apssetObject:@"def.mp3"forKey:@"sound"];
[jsonDicsetObject:apsforKey:@"aps"];
NSString*strJson=[jsonDicJSONString];
另一个,其中operation.responseString就是下面那串json字符串,通过它进行解析:
NSDictionary*resultDictionary=[operation.responseStringobjectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
NSLog(@"%@ItemsFound!",[resultDictionaryobjectForKey:@"weatherinfo"]);
//{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"3","WD":"北风","WS":"3级","SD":"24%","WSE":"3","time":"11:25","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1028"}}
self.weaLableInfo.text=[NSStringstringWithFormat:@"城市:%@,温度:%@",[[resultDictionaryobjectForKey:@"weatherinfo"]objectForKey:@"city"],[[resultDictionaryobjectForKey:@"weatherinfo"]objectForKey:@"temp"]];
三十七:三十五:afnetworking2.0运用,结合json,引入afnetworking文件后,引入头文件就可以使用,支持arc
-(IBAction)JsonAction:(UIBarButtonItem*)sender{
NSString*weatherUrl=[NSStringstringWithFormat:@"%@%@.html",BaseURLString,self.UrlString];
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManagermanager];
manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/html"];
[managerGET:weatherUrlparameters:nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject){
NSDictionary*resultDictionary=[operation.responseStringobjectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
NSLog(@"%@ItemsFound!",[resultDictionaryobjectForKey:@"weatherinfo"]);
//{"weatherinfo":{"city":"北京","cityid":"101010100","temp":"3","WD":"北风","WS":"3级","SD":"24%","WSE":"3","time":"11:25","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1028"}}
self.weaLableInfo.text=[NSStringstringWithFormat:@"城市:%@,温度:%@",[[resultDictionaryobjectForKey:@"weatherinfo"]objectForKey:@"city"],[[resultDictionaryobjectForKey:@"weatherinfo"]objectForKey:@"temp"]];
}failure:^(AFHTTPRequestOperation*operation,NSError*error){
NSLog(@"Error:%@",error);
}];
}
带post参数到服务端
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManagermanager];
//申明返回的结果是json类型
manager.responseSerializer=[AFJSONResponseSerializerserializer];
//申明请求的数据是json类型
manager.requestSerializer=[AFJSONRequestSerializerserializer];
//如果报接受类型不一致请替换一致text/html或别的
manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/html"];
//传入的参数
NSDictionary*parameters=@{@"1":@"XXXX",@"2":@"XXXX",@"3":@"XXXXX"};
//你的接口地址
NSString*url=@"http://xxxxx";
//发送请求
[managerPOST:urlparameters:parameterssuccess:^(AFHTTPRequestOperation*operation,idresponseObject){
NSLog(@"JSON:%@",responseObject);
}failure:^(AFHTTPRequestOperation*operation,NSError*error){
NSLog(@"Error:%@",error);
}];
相关文章推荐
- IOS开发基础知识--碎片10
- IOS开发基础知识--碎片15
- IOS开发基础知识--碎片27
- IOS开发基础知识--碎片18
- IOS开发基础知识--碎片30
- IOS开发基础知识--碎片11
- IOS开发基础知识--碎片22
- IOS开发基础知识--碎片29
- IOS开发基础知识--碎片2
- IOS开发基础知识--碎片1
- IOS开发基础知识--碎片4
- IOS开发基础知识--碎片3
- IOS开发基础知识--碎片16
- IOS开发基础知识--碎片19
- IOS开发基础知识--碎片8
- IOS开发基础知识--碎片14
- IOS开发基础知识碎片-导航
- IOS开发基础知识--碎片12
- IOS开发基础知识--碎片20
- IOS开发基础知识--碎片26