zoukankan      html  css  js  c++  java
  • ios学习流水账1

    1、UIImageview设边框、圆角

    需要引QuartzCore/QuartzCore.h>

    C代码  收藏代码
    1. //设UIImageView边框  
    2.         CALayer *layer = [m_imgView layer];  
    3.         [layer setMasksToBounds:YES];  
    4.         layer.cornerRadius = 10.0;//设圆角  
    5.         [layer setBorderWidth:1];  
    6.         [layer setBorderColor:[[UIColor blackColor] CGColor]];  

    2、bounds属性和frame属性区别

      frame指的是:该view在父view坐标系统中的位置和大小.

      bounds指的是:该view在本身坐标系统中的位置和大小.

    3、导航navigationController设置按钮背景图片

    C代码  收藏代码
    1. UIImage *backImage = [UIImage imageNamed:@"ip_bt-back.png"];  
    2.     UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, backImage.size.width, backImage.size.height)];  
    3.     [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];  
    4.     [backBtn setBackgroundImage:backImage forState:UIControlStateNormal];  
    5.     UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];  
    6.     self.navigationItem.leftBarButtonItem = leftBtn;  

    4、ios5加载自定义单元格

    C代码  收藏代码
    1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    2. {  
    3.     static NSString *CellIdentifier = @"priceRecordCell";  
    4.     //加载nib  
    5.     static BOOL nibsRegistered = NO;    
    6.     if (!nibsRegistered) {    
    7.         UINib *nib = [UINib nibWithNibName:@"PriceRecordCell" bundle:nil];    
    8.         [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];    
    9.         nibsRegistered = YES;    
    10.     }    
    11.   
    12.     PriceRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    13.     if (cell == nil) {  
    14.         cell = [[PriceRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
    15.     }  
    16.     // Configure the cell...  
    17.     cell.nickName.text = @"爱我中华";  
    18.     cell.priceLab.text = @"价格 ¥100.00";  
    19.     cell.lastTimeLab.text = @"2012-10-15 18:00:08";  
    20.     return cell;  
    21. }  

    5、tableview单元格分割线设置

        //不需要分割线
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    6、ios4工程在ios5上运行会出现ARC问题,在Compile Sources 下的Compiler Flags属性中添加-fno-objc-arc,可解决该问题 。

    7、UIImageView 上添加点击事件

      

    C代码  收藏代码
    1. UIImageView *imageView =[[UIImageView alloc ]initWithFrame :CGRectMake (100 , 100 , 200 , 200 )];  
    2.   
    3.  imageView. image =[ UIImage imageNamed : @"test.png"];  
    4.   
    5.   //事件可用设置  
    6.   
    7.  imageView. userInteractionEnabled = YES ;  
    8.   
    9.  UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (clickImage)];  
    10.   
    11.  [imageView addGestureRecognizer :tap];  

    8、ios5 UIView 设圆角

    需添加#import <QuartzCore/QuartzCore.h>

    centerView.layer.cornerRadius = 6.0f;

    9、获取文件路径

    C代码  收藏代码
    1. //获取沙盒路径  
    2.     NSString *sandboxPath = NSHomeDirectory();  
    3.     NSLog(@"sandboxPath :%@",sandboxPath);  
    4.     NSString *documentPath = [sandboxPath stringByAppendingPathComponent:@"Documents"];  
    5.     NSLog(@"documentPath : %@",documentPath);  
    6.     //获取沙盒中的文件目录  
    7.     NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    8.     //遍历得到的路径  
    9.     for (int i=0; i<[documentDirectories count]; i++) {  
    10.         NSLog(@"%@",[documentDirectories objectAtIndex:i]);  
    11.     }  

    10、自定义单元格中,如果自定义单元格中有按钮点击事件,不可设置以下属性,会阻塞按钮事件解发。 (2012-10-16)

    cell_1.userInteractionEnabled = NO;//单元格不可点击

    11、内存过低警告,清除内存

       通过消息通知方式提示并清除

    C代码  收藏代码
    1. - (id)init{  
    2.      
    3.    //当内存过低时,清空内存  
    4.     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];  
    5.     [nc addObserver:self selector:@selector(clearCache) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];  
    6.   
    7. }  

    12、NSBundle可以获取当前程序的资源,得到它,可以获取当前资源中的相关文件,并进行操作。

    C代码  收藏代码
    1. //NSBundle获取当前应用资源  
    2.     NSBundle *appBundle = [NSBundle mainBundle];  
    3.     //bundle得到文件  
    4.     NSString *path = [appBundle pathForResource:@"first" ofType:@"png"];  
    5.     if (path != nil) {  
    6.         NSLog(@"path : %@",path);  
    7.     }  
    8.     //bundle得到类  
    9.     Class class = [appBundle classNamed:@"Test"];  
    10.     Test *tinstance = [[class alloc] init];  
    11.     [tinstance test];  
    12.     //bundle加载xib  
    13.     [appBundle loadNibNamed:@"ThirdViewController" owner:self options:nil];  
    14.   
    15.    //bundle中还有很多其它方法,可查api  

    13、应用状态、状态切换,通过它可了解应用在运行时的各个状态。

       常见应用中多种状态:末运行--激活--末激活--后台运行--暂停--激活

    C代码  收藏代码
    1. //启动应用时调用   
    2. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    3. {  
    4. //......    NSLog(@"====didFinishLaunchingWithOptions.........%@",NSStringFromSelector(_cmd));  
    5.     return YES;  
    6. }  
    7.   
    8. //按下主窗口按钮时调用(末激活)  
    9. - (void)applicationWillResignActive:(UIApplication *)application  
    10. {  
    11.     NSLog(@"====applicationWillResignActive");  
    12. }  
    13.   
    14. //按下主窗口按钮时调用(后台运行,末激活,5秒后进入暂停)  
    15. - (void)applicationDidEnterBackground:(UIApplication *)application  
    16. {  
    17.   //进行入后运行时,该方法是保存数据的最佳方法  
    18.   //[object saveChanges];  
    19.     NSLog(@"====applicationDidEnterBackground");  
    20. }  
    21.   
    22. //按主窗口按钮退出后,再按下应用图标时调用(激活)  
    23. - (void)applicationWillEnterForeground:(UIApplication *)application  
    24. {  
    25.     NSLog(@"====applicationWillEnterForeground");  
    26. }  
    27.   
    28. //按主窗口按钮退出后,再按下应用图标时调用(激活)  
    29. - (void)applicationDidBecomeActive:(UIApplication *)application  
    30. {  
    31.     NSLog(@"====applicationDidBecomeActive");  
    32. }  
    33.   
    34. //ios4之前实现该方法,在该方法保存数据最佳  
    35. - (void)applicationWillTerminate:(UIApplication *)application  
    36. {  
    37.     NSLog(@"====applicationWillTerminate");  
    38. }  

    14、沙盒理解

    每个ios应用都有自己的应用沙盒(application sandbox),应用沙盒就是文件系统目录,但与文件系统其他部分隔离。应用必须待在自己的沙盒里,其他应用不能访问该沙盒。

    15、navigation导航

    C代码  收藏代码
    1.  //设置Navigation Bar背景  
    2.     UIImage *title_bg = [UIImage imageNamed:@"ip_titelbar.png"];  
    3.     [self.navigationController.navigationBar setBackgroundImage:title_bg forBarMetrics:UIBarMetricsDefault];  
    4.     UIView *_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 360, 40)];  
    5.     UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 20)];  
    6.     titleLab.text = @"快速竞拍";  
    7.     titleLab.textColor = [UIColor redColor];  
    8.     titleLab.font = [UIFont fontWithName:@"Arial" size:18];  
    9.     titleLab.backgroundColor = [UIColor clearColor];  
    10.     [_view addSubview:titleLab];  
    11.     resultLab = [[UILabel alloc] initWithFrame:CGRectMake(122, 20, 100, 20)];  
    12.     resultLab.backgroundColor = [UIColor clearColor];  
    13.     resultLab.font = [UIFont fontWithName:@"Arial" size:14];  
    14.     [_view addSubview:resultLab];  
    15.     //刷新按钮  
    16.     UIButton *refreshBtn = [[UIButton alloc] initWithFrame:CGRectMake(255, 8, 50, 30)];  
    17.     [refreshBtn setBackgroundImage:[UIImage imageNamed:@"ip_bt_shuaxin.png"] forState:UIControlStateNormal];  
    18.     [refreshBtn addTarget:self action:@selector(refurbish) forControlEvents:UIControlEventTouchUpInside];  
    19.     [_view addSubview:refreshBtn];  
    20.     self.navigationItem.titleView = _view;  
    21.   
    22. //显示导航  
    23. //    self.navigationController.navigationBarHidden = NO;   

    16、ipad中UITabBarController添加ViewController。

    C代码  收藏代码
    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2. {  
    3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    4.     // Override point for customization after application launch.  
    5.   
    6.     MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];  
    7.     UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];  
    8.   
    9.     DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];  
    10.     UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];  
    11.   
    12.     masterViewController.detailViewController = detailViewController;  
    13.       
    14.     self.splitViewController = [[UISplitViewController alloc] init];  
    15.     self.splitViewController.delegate = detailViewController;  
    16.     self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];  
    17.       
    18.     //首页barTab  
    19.     UITabBarController *tabBarController = [[UITabBarController alloc] init];  
    20.     self.splitViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"f.jpg"] tag:0];  
    21.     //购物车barTab  
    22.     CartViewController *cartVC = [[CartViewController alloc] initWithNibName:@"CartViewController" bundle:nil];  
    23.     cartVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"购物车" image:[UIImage imageNamed:@"f.jpg"] tag:0];  
    24.     //条码购barTab  
    25.     BarCodeViewController *barcodeVC = [[BarCodeViewController alloc] initWithNibName:@"BarCodeViewController" bundle:nil];  
    26.     barcodeVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"条码购" image:[UIImage imageNamed:@"f.jpg"] tag:0];  
    27.     //更多barTab  
    28.     MoreViewController *moreVC = [[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil];  
    29.     moreVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"更多" image:[UIImage imageNamed:@"f.jpg"] tag:0];  
    30.       
    31.    //controller数组  
    32.     NSArray *controllers = [NSArray arrayWithObjects:self.splitViewController,cartVC,barcodeVC,moreVC, nil];  
    33.     tabBarController.viewControllers = controllers;  
    34.     self.window.rootViewController = tabBarController;  
    35.   
    36. //    self.window.rootViewController = self.splitViewController;  
    37.     [self.window makeKeyAndVisible];  
    38.     return YES;  
    39. }  

    17、如何获取应用存放文件根路径:

    C代码  收藏代码
    1. //根路径  
    2.    NSString *homePath = [[NSBundle mainBundle] executablePath];  
    3.    NSArray *strings = [homePath componentsSeparatedByString: @"/"];  
    4.    NSString *executableName  = [strings objectAtIndex:[strings count]-1];     
    5.    NSString *baseDirectory = [homePath substringToIndex:  
    6.                               [homePath length]-[executableName length]-1];     
    7.    NSString *fileName = [NSString stringWithFormat:@"%@/test.txt",baseDirectory];  
    8.    NSLog(@"filePath: %@",fileName);  

    18、block类似匿名函数

    以为例子:

    C代码  收藏代码
    1. static int outA = 8;  
    2.         static NSString *c=@"";  
    3.         int(^myPtr)(int,NSString *)=^(int a,NSString *b){  
    4.             c=b;  
    5.             return outA+a;  
    6.         };  
    7.         outA = 5;//改变outA  
    8.         int result = myPtr(2,@"test");//结果为10,outA是复制的,后面改变outA无效  
    9.         NSLog(@"result : %i",result);  

    19、performSelector的应用,它可直接调用实例的方法,能延迟执行方法时间。

    C代码  收藏代码
    1. - (void) test:(NSString *) str;  
    2.   
    3. - (void) test:(NSString *) str{  
    4.     NSLog(@"you input is : %@",str);  
    5. }  
    6.   
    7. //调用方法  
    8. [self performSelector:@selector(test:) withObject:@"dwen"];  
    9. [self performSelector:@selector(test:) withObject:@"wen" afterDelay:2.0f];//两秒后执行  

    20、UIButton中addTarget中传参问题,该事件可以通过setTag方法进行传整数。

    C代码  收藏代码
    1.  UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];    
    2. //action参数中写入事件执行方法    
    3. [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];    
    4. //在button的tag中添加你需要传递的参数   
    5. [button setTag:100];   
    6.   
    7. //action方法    
    8. -(void)action:(id)sender{    
    9.   int i = [sender tag];  
    10. }  

     

    21、沙盒中包含三个文件夹,Documents 、Library和tmp

    C代码  收藏代码
    1. //获取Documents目录  
    2.         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);  
    3.         NSString *documentDirectory = [paths objectAtIndex:0];  
    4.          NSLog(@"%@",documentDirectory);  
    5.         //获取当前应用中文件路径  
    6.         NSString *filename = [documentDirectory stringByAppendingPathComponent:@"w12122.jpg"];  
    7.         NSLog(@"%@",filename);  
    8.           
    9.         //获取tmp目录  
    10.         NSString *tempPath = NSTemporaryDirectory();  
    11.         NSString *tempFile = [tempPath stringByAppendingPathComponent:@"w12122.jpg"];  
    12.         NSLog(@"tempFile :%@",tempFile);  

     

    22、NSString截取字符串

    NSString *str = @"B12121.jpg";
            NSRange range = [str rangeOfString:@"."];
            NSLog(@"%i",range.location);
            NSLog(@"%@",[str substringToIndex:range.location]);

     23、UINavigationBar

    C代码  收藏代码
    1. //创建导航栏集合  
    2.  UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];  
    3.  //创建一个左边按钮  
    4.  UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回"  
    5.                                                                style:UIBarButtonItemStyleBordered  
    6.                                                                target:self  
    7.                                                                action:@selector(back)];  
    8.  //设置导航栏内容  
    9.  [navigationItem setTitle:@"拍品列表"];  
    10.  //把导航栏集合添加入导航栏中,设置动画关闭  
    11.  [navigationBar pushNavigationItem:navigationItem animated:NO];  
    12.  //把左右两个按钮添加入导航栏集合中  
    13.  [navigationItem setLeftBarButtonItem:leftButton];  

    24、在navigationController中添加右边多个按钮。效果图:



     代码如下:

    Object-c代码  收藏代码
    1. //添加导航右边按钮  
    2.     UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0015044)];  
    3.     [tools setTintColor:[self.navigationController.navigationBar tintColor]];  
    4.     [tools setAlpha:[self.navigationController.navigationBar alpha]];  
    5.     NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];  
    6.     UIBarButtonItem *firstBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSpecial)];  
    7.     [buttons addObject:firstBtn];  
    8.     [tools setItems:buttons animated:NO];  
    9.     UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools];  
    10.     self.navigationItem.rightBarButtonItem = myBtn;  

    25、循环删除uiview

    Object-c代码  收藏代码
    1. for (UIView *subview in scrollView.subviews) {  
    2.         if ([subview isKindOfClass:[CatalogView class]]) {  
    3.             [subview removeFromSuperview];  
    4.         }  
    5.     }  

    26、ios4以前内存管理 



     

    00

  • 相关阅读:
    OAuth
    PHP获取客户端的真实IP
    负载均衡----实现配置篇(Nginx)
    在线时间戳转换
    使用curl进行模拟登录
    定时任务
    Matplotlib使用教程
    CentOS7.X安装PHP
    Python虚拟环境的搭建与使用
    CentOS7.X安装openssl
  • 原文地址:https://www.cnblogs.com/bmate/p/3183366.html
Copyright © 2011-2022 走看看