IOS开发基础知识--碎片6

三十三:IOS多视图跳转方法

第一种:

跳转:[self presentModalViewController:control animated:YES];

返回:[self dismissModalViewControllerAnimated:YES];

第二种:

跳转:[self.navigationController pushViewController:subTableViewController  animated:YES];

返回:[self.navigationController popViewControllerAnimated:YES];

第三种:自己控制: [self.view addSubview:<#(UIView *)#>] [self.view removeFromSuperview];

注意:pushViewController和popViewController进行视图间的切换,就必须要求当前视图是个NavigationController,

第四种:
// 根据Segue ID 执行跳转  
[self performSegueWithIdentifier:@"contactList" sender:nil];

三十四:presentModalViewController与dismissModalViewControllerAnimated注意事项

在实际开发中,如果要弹出视图:

我们常用到presentModalViewController方法和dismissModalViewControllerAnimated方法。

presentModalViewController:弹出视图

dismissModalViewControllerAnimated:隐藏视图

弹出视图:

FeedbackViewController *feedbackViewController = [[FeedbackViewController alloc]initWithNibName:@"FeedbackViewController" bundle:nil];

    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:feedbackViewController];

    [self presentModalViewController:navigationController animated:YES];

隐藏视图:

[self dismissModalViewControllerAnimated: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-27 14:01:23.666 WTV[1627:11303] -More--viewWillDisappear----

2012-12-27 14:01:23.672 WTV[1627:11303] -Feed--viewWillAppear----

2012-12-27 14:01:24.086 WTV[1627:11303] -Feed--viewDidAppear----

2012-12-27 14:01:24.087 WTV[1627:11303] -More--viewDidDisappear----

2012-12-27 14:01:25.745 WTV[1627:11303] -Feed--viewWillDisappear----

2012-12-27 14:01:25.745 WTV[1627:11303] -More--viewWillAppear----

2012-12-27 14:01:26.156 WTV[1627:11303] -More--viewDidAppear----

2012-12-27 14:01:26.157 WTV[1627:11303] -Feed--viewDidDisappear----

当我们信心慢慢,庆幸我们可以了解了这两个方法时,悲剧发生了:

4.苹果官方已经把这两个方法 Deprecated in iOS 6.0. 了

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated;

- (void)dismissModalViewControllerAnimated:(BOOL)animated;

取而代之的是:

[self presentViewController:navigationController
                       animated:YES
                     completion:^(void){
                         // Code
                     }];


[self dismissViewControllerAnimated:YES
                             completion:^(void){
                                 // Code
                             }];


新接口的差别是提供了一个参数,允许你传入一个block。这个block的回调方法在VC的viewWillDisappear方法后调用。也就是被隐藏的VC对象被释放后运行回调。

这样做的好处:可以方便做多个UI效果之间的衔接和转换。

 三十五:视图跳载的几种动画

        BaiDuViewController* baiduController=[mainStoryboard instantiateViewControllerWithIdentifier:@"baiduStoryboard"];
        baiduController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:baiduController animated:YES completion:^{
            
        }];
  1. UIModalTransitionStyleCoverVertical  //新视图从下向上出现 
  2. UIModalTransitionStyleFlipHorizontal //以设备的长轴为中心翻转出现 
  3. UIModalTransitionStyleCrossDissolve  //渐渐显示 
  4. UIModalTransitionStylePartialCurl    //原视图向上卷起 
 

三十六:JSONKit的使用方法

json开源的类库有很多,其中JSONKit库是非常简单易用而且效率又比较高的,重要的JSONKit适用于ios 5.0以下的版本。

下载地址:https://github.com/johnezang/JSONKit

使用JSONKit库来解析json文件,只需要下载JSONKit.h 和JSONKit.m添加到工程中,设置支持arc,在项目中build phases中的compile sources,选择jsonkit.m然后确认键输入-fno-objc-arc

 #import "JSONKit.h"

//假设 strJson 是网络上接收到的 json 字符串,
NSString *strJson = @"[{\"Id\": 1,\"BrandName\": \"爱马仕\" },{\"Id\": 2,\"BrandName\": \"安娜苏\"}]"; 
    NSArray *arrlist=[strJson objectFromJSONString];
    NSLog(@"%d",[arrlist count]);
    for (int i=0; i<[arrlist count]; i++) {
        NSDictionary *item=[arrlist objectAtIndex:i];
        NSString *BrandName=[item objectForKey:@"BrandName"];
        NSLog(@"%@",BrandName);
    }

字典arrlist便是解析好的json文件了。

JSONKit库也可以用来生成json文件

NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
NSMutableDictionary *alert = [NSMutableDictionary dictionary]
;NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[alert setObject:@"a msg come!" forKey:@"body"];
[aps setObject:alert forKey:@"alert"];
[aps setObject:@"3" forKey:@"bage" ];
[aps setObject:@"def.mp3" forKey:@"sound"];
[jsonDic setObject:aps forKey:@"aps"];
NSString *strJson = [jsonDic JSONString];

另一个,其中operation.responseString就是下面那串json字符串,通过它进行解析:

        NSDictionary* resultDictionary=[operation.responseString objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        NSLog(@"%@ Items Found!",[resultDictionary objectForKey:@"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=[NSString stringWithFormat:@"城市:%@,温度:%@",[[resultDictionary objectForKey:@"weatherinfo"]objectForKey:@"city"],[[resultDictionary objectForKey:@"weatherinfo"]objectForKey:@"temp"]];

三十七:三十五:afnetworking2.0运用,结合json,引入afnetworking文件后,引入头文件就可以使用,支持arc

- (IBAction)JsonAction:(UIBarButtonItem *)sender {
    NSString* weatherUrl=[NSString stringWithFormat:@"%@%@.html",BaseURLString,self.UrlString];
    
    AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:weatherUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        NSDictionary* resultDictionary=[operation.responseString objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        NSLog(@"%@ Items Found!",[resultDictionary objectForKey:@"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=[NSString stringWithFormat:@"城市:%@,温度:%@",[[resultDictionary objectForKey:@"weatherinfo"]objectForKey:@"city"],[[resultDictionary objectForKey:@"weatherinfo"]objectForKey:@"temp"]];
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
    }];
    
}

带post参数到服务端

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//申明返回的结果是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer=[AFJSONRequestSerializer serializer];
//如果报接受类型不一致请替换一致text/html或别的
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//传入的参数
NSDictionary *parameters = @{@"1":@"XXXX",@"2":@"XXXX",@"3":@"XXXXX"};
//你的接口地址
NSString *url=@"http://xxxxx";
//发送请求
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

 

你可能感兴趣的:(ios开发)