现在的位置: 首页 > 综合 > 正文

总结代码

2013年12月06日 ⁄ 综合 ⁄ 共 6105字 ⁄ 字号 评论关闭
1、页面跳转

复制代码
  //隐藏底部的菜单栏
  self.hidesBottomBarWhenPushed = YES; 
  //  初始化页面并且跳转
    Newpage*newpage=[[Newpage alloc]init];
       [self.navigationController pushViewController:about animated:YES];
  //记得返回的时候显示底部菜单栏
    self.hidesBottomBarWhenPushed = NO;
    [newpage release];
    about=nil;
复制代码
2、网络请求

复制代码
    NSString *urlstring=@"www.baidu.com";

   NSError *error =nil;//定义一个error类型的变量存储错误
    //获取路径对象
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
    //获取完整路径
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"zhibin.plist"];
    NSFileManager *fm=[NSFileManager defaultManager];//文件管理
 
    //判断文件是否存在,如果存在则删除再重建,不存在 就创建
   if ([fm fileExistsAtPath:filename]) {
        if(reload){
         //如果文件存在且是应用开启的第一次加载则删除上次的缓存 再重新创建
        [fm  removeItemAtPath:filename error:&error];
            [fm createFileAtPath:filename contents:nil attributes:nil];}
       reload=false;
      
   }   else {
       //如果应用是安装完后第一次打开 则直接创建
     [fm createFileAtPath:filename contents:nil attributes:nil];
    } 
    
//定义一个url类型的变量 然后把string类型的变量用urlwithstring函数转换为url
 NSURL *url=[NSURL URLWithString:urlstring];
 
//定义一个request请求  
NSURLRequest *request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:10.0f];
   //定义一个data类型来存储request类型的数据
    NSData *respone=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    //定义一个string类型的把data类型枪支转换成字符类型并用utf8去转码
    NSString *str=[[NSString alloc]initWithData:respone encoding:NSUTF8StringEncoding];
   //字符串替换
    str = [str stringByReplacingOccurrencesOfString:@"1" withString:@"2"];

    NSData *res = [str dataUsingEncoding:NSUTF8StringEncoding];
  
  
  if (res!=nil)//证明有返回数据
  
  {
     
   //定义一个字典类型的变量把data类型枪支转成字典类型
   
     NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:res options:NSJSONReadingMutableLeaves error:&error];
    
    //定义一个数组把查找字典中的根  把根的子孩子写入数组
   
     NSMutableArray *templist = [jsonObject objectForKey:@"root"];
  }
复制代码
3、显示本地图片

 UIImage *image = [UIImage imageNamed:@"zhibin.png"]; 
                    [tmpImageArray addObject:image];
4、显示网络图片

// 指定URL生成UIImage
 NSData *dt = [NSData dataWithContentsOfURL: 
[NSURL URLWithString:@"http://xxx/aa.png"]]; 
UIImage *image = [[UIImage alloc] initWithData:dt]; 
5、全局变量

appdelegate.h

@property BOOL netstatus;
.m文件

 AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];

 app.netstatus=YES;/
6、用户退出也可以保存的数据

复制代码
  //添加一个退出也可以保存的用户变量
    NSUserDefaults *set=[NSUserDefaults standardUserDefaults];
    BOOL first=[set boolForKey:@"first"];
    if(!first)
    {
    
        [set setBool:YES forKey:@"first"];
        
        [set setInteger:1forKey:@"one"]; 
        [set setBool:NO forKey:@"bool"];
        [set setInteger:3 forKey:@"three"];
        [set  setInteger:4 forKey:@"four"];
        [set  setBool:YES forKey:@"five"];

        
    }
复制代码
 NSUserDefaults *set=[NSUserDefaults standardUserDefaults];
    BOOL temp=[set  boolForKey:@"five"];
 6、allertview

        UIAlertView *myalert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"网络不存在" delegate:self cancelButtonTitle:@"确认"
        otherButtonTitles:nil,nil];
        [myalert show];
        [myalert release];
7、图片下载到本地
.h文件

-(UIImage *)getImageFromURL:(NSString *)fileURL;
-(void)saveImage:(UIImage *)tupian withFileName:(NSString *)imageName  ofType:(NSString *)extension inDirectory:(NSString *)directoryPath;
-(UIImage *)loadImage:(NSString *)fileName  ofType:(NSString *)extension inDirectory:(NSString *)directoryPath;
.m文件

复制代码
     UIImage *imageFromURL=[self getImageFromURL:picurl];
                    NSString *imagename=[xinwenid  stringByAppendingFormat:@"_list"];
                    
                    [self saveImage:imageFromURL withFileName:imagename ofType:@"jpg" inDirectory:documentsDiretoryPath];
                    UIImage *imageformweb=[self loadImage:imagename ofType:@"jpg"  inDirectory:documentsDiretoryPath];
  

                    [tmpImageArray addObject:imageformweb];
复制代码
 

复制代码
//下载图片
-(UIImage *)getImageFromURL:(NSString *)fileURL
{
    UIImage *result;
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result=[UIImage imageWithData:data];
    return result;
}
//保存图片
-(void)saveImage:(UIImage *)tupian withFileName:(NSString *)imageName  ofType:(NSString *)extension inDirectory:(NSString *)directoryPath{
    if ([[extension lowercaseString]isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(tupian) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",imageName,@"png"]] options:NSAtomicWrite error:nil];    
    }else if ([[extension lowercaseString]isEqualToString:@"jpg"]||[[extension lowercaseString]isEqualToString:@"jpeg"] ) 
    {
        [UIImageJPEGRepresentation(tupian, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",imageName,@"jpg"]] options:NSAtomicWrite error:nil];    
        
    }else 
    {
        NSLog(@"不认识拓展名");
    }
    
}
//加载图片
-(UIImage *)loadImage:(NSString *)fileName  ofType:(NSString *)extension inDirectory:(NSString *)directoryPath
{
    UIImage *result=[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@",directoryPath,fileName,extension]];
    return result;
    
    
}
复制代码
 8、页面左右滑动

  UISwipeGestureRecognizer *recognizer;
    recognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom5:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:recognizer];
复制代码
-(void)handleSwipeFrom5:(UISwipeGestureRecognizer*)recognizer
{
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight)
    {
        
        
        self.hidesBottomBarWhenPushed = YES;      
       
        [self.navigationController popViewControllerAnimated:YES];
        
        
        
//        self.tabBarController.selectedIndex=0;
//        self.hidesBottomBarWhenPushed = NO; 


    }

}
复制代码

9、 .m文件里面创建控件

复制代码
   UILabel *introduce = [[UILabel alloc] initWithFrame:CGRectMake(15, 230, 290, 160)];
    [introduce setFont:[UIFont fontWithName:@"Helvetica" size:17]];
    [introduce setTextColor:[UIColor grayColor]];
    [introduce setNumberOfLines:0];
    
    [introduce setBackgroundColor:[UIColor clearColor]];
    introduce.lineBreakMode = UILineBreakModeWordWrap;
    introduce.text = @" 1111\n\n    222\n\n 333\n\n   4444";

    [self.view addSubview:introduce];
    
    UIImageView *logoimg1 = [[UIImageView alloc] initWithFrame:CGRectMake(55, 15, 200, 200)];
    [logoimg1 setImage:[UIImage imageNamed:@"aboutlogo.png"]];
    [self.view addSubview:logoimg1];
  
复制代码
 10、数组

复制代码
    NSMutableArray *items=[[NSMutableArray alloc]init];
    [items addObject:@"1"];
     
    [items addObject:@"2"];
    
    [items insertObject:@"0" atIndex:0];

    
   int i=[items count];
    
 
   [items addObject:[NSNull null]];
        
    
    for (int i=0; i<items.count; i++) {
        NSLog(@"%@",[items objectAtIndex:i]);
    }
    [items release];
    items=nil;
复制代码
 11、判断字符串长度

    NSString *nsstring=@"hello  world";
    int len=[nsstring  length];
    int len=[@"hello world" length];
    nsstring=[[NSString alloc]initWithString:@"hello world"];
    int len=[nsstring length];

抱歉!评论已关闭.