zoukankan      html  css  js  c++  java
  • ios 常用技巧

    1、将日志写入文件

        在appDeleagate.m中的didFinishLau....方法里,加上

     // 当真机连接Mac调试的时候把这些注释掉,否则log只会输入到文件中,而不能从xcode的监视器中看到。如果是真机就保存到Document目录下的drm.log文件中
        UIDevice *device = [UIDevice currentDevice];   
        if (![[device model] isEqualToString:@"iPhone Simulator"]) {
            // 开始保存日志文件
            [self redirectNSlogToDocumentFolder];
        }

    在文件中加入方法:

    //重定向log输出
    - (void)redirectNSlogToDocumentFolder
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
         NSString *documentDirectory = [paths objectAtIndex:0];
        NSString *fileName = [NSString stringWithFormat:@"dr.log"];// 注意不是NSData!
        NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
        
        // 先删除已经存在的文件
        NSFileManager *defaultManager = [NSFileManager defaultManager];
        [defaultManager removeItemAtPath:logFilePath error:nil];
        // 将log输入到文件
        freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
        freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
    }

    最后配置共享文件夹:

            在应用程序的Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES。将您希望共享的文件放在应用程序的Documents目录。一旦设备插入到用户计算机,iTunes 9.1就会在选中设备的Apps标签中显示一个File Sharing区域。此后,用户就可以向该目录添加文件或者将文件移动到桌面计算机中。如果应用程序支持文件共享,当文件添加到Documents目录后,应用程序应该能够识别并做出适当响应。例如说,应用程序可以将新文件的内容显示界面上。请不要向用户展现目录的文件列表并询问他们希望对文件执行什么操作。     

           就是说,一旦设备连接上电脑,可以通过iTune查看指定应用程序的共享文件夹,将文件拷贝到你的电脑上看。

    这是简单的实现日志输出到文件,如果要更详细的显示,可以参考http://www.cnblogs.com/alario/archive/2012/03/27/2419710.html

    2、记录一个操作执行的时间

    CFAbsoluteTime startTime,endTime,totalTime;//单位是s
    startTime = CFAbsoluteTimeGetCurrent();
    .....//要执行的方法
    endTime = CFAbsoluteTimeGetCurrent();
    totalTime = (endTime - startTime)*1000;//转换成ms

    或者是:

    NSTimeInterval    _connectStartTime, _connectEndTime; //单位是s
    _connectStartTime = [[NSDate date] timeIntervalSince1970]; 
    ......
    _connectEndTime= [[NSDate date] timeIntervalSince1970]; 


    3、NSString与const char*相互转换

    NSString *strNSString;
    const char *pConstChar;
    strNSString = [[NSString alloc] initWithUTF8String:pConstChar];
    pConstChar = [strNSString UTF8String];

    4、NSString去空格、换行符

    NSString *str = [str2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//去掉换行符和空格
    NSString *str1= [str2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];//只去掉空格

    5、arc,非arc混用

    如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签。

    如果你的项目使用的是 ARC 模式,则为非 ARC 模式的代码文件加入 -fno-objc-arc 标签。

    添加标签的方法:

    1. 打开:你的target -> Build Phases -> Compile Sources.
    2. 双击对应的 *.m 文件
    3. 在弹出窗口中输入上面提到的标签 -fobjc-arc / -fno-objc-arc
    4. 点击 done 保存

    6、按钮点击的独占性

    对于测试组的同事经常提的bug:两个手指同时点击界面上的多个可点击的视图而出现各种错误。
    有官方的解决方法,将不允许同时点击的视图都设置下参数:
    [view setExclusiveTouch:YES];

    7、voiceover

    #ifdef SUPPORT_VOICEOVER
         self.isAccessibilityElement = YES; 这句话可以把任意元素设为voiceover可识别的
         self.accessibilityLabel = voice;
    #endif
     
    8、在一个UILabel 使用不同的颜色或不同的字体来体现字符串
    在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的attributedText, 使用不同颜色和不同字体的字符串,我们可以使用NSAttributedText 和 NSMutableAttributedText 类来实现。
    self.title = @"For iOS 6 & later";
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
    attrLabel.attributedText = str;

    9、UITableView dataSource must return a cell from tableView:cellForRowAtIndexPa 

    错误意思是:必须返回一个cell实例;

    可能原因:

    1. XIB没有连接正确(datasource, delegate), 这种情况比较多;

    2. 自定义的tableViewCell未初始化;

    3. 在cellForRowAtIndexPath()方法中创建cell用的是[tableView dequeueReusableCellWithIdentifier:identifier];而这里的identifier我并没有在xib中的cell中填写

    4、The problem was solved by assigning the tableView.datasource and tableView.delegate to nil in dealloc.

    The table view was most likely still pending dealloc when the container view controller was deallocated, and it could not call my cellForRowAtIndexPath implementation. At least, I was able to reproduce the problem (and the fix).

    10、ios7 ios8模态实现半透明

               //源Controller中跳转方法实现
                MKDialogController *controller = [[MKDialogController alloc] init];
                //controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
                if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
                    controller.providesPresentationContextTransitionStyle = YES;
                    controller.definesPresentationContext = YES;
                    controller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
                    [self presentViewController:controller animated:YES completion:nil];
                } else {
                    self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
                    [self presentViewController:controller animated:NO completion:nil];
                    self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
                }

    11、遇到UIScollView  crash:

    0 libobjc.A.dylib 0x39143f66 objc_msgSend + -1
    1 UIKit 0x2e327dd0 -[UIScrollView setContentOffset:]

    原因: It turned out to be because we were animating a controller with [controller setContentOffset:newPt animated:YES], and we implemented the scrollViewDidScroll delegate method on the controller. Clicking a button on the screen let you advance to another controller, so if a user managed to click the button while the animation was in progress, we would hit the original poster's crash.

    解决:The solution is simply to set the delegate to nil in dealloc.

    要点:对于UIScrollView、tableView等,在view将消失时或在dealloc里一定要手动将delegate = nil 

    12、消失键盘

    ios9以上 键盘的弹出时机有了变化,在所有地方消除键盘:

    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
  • 相关阅读:
    Spark介绍与环境搭建
    Kafka基本操作
    Hadoop的HDFS概述
    hadoop环境搭建
    常用小工具
    mac机
    Eclipse使用
    微信公众号开发
    PM2
    JS 零散知识点
  • 原文地址:https://www.cnblogs.com/wyqfighting/p/3298754.html
Copyright © 2011-2022 走看看