iOS 开发过程中记录的一些小知识(持续更新...)

以下内容是在开发中总结的一些小知识,如果有错,请各位看官提出,谢谢!


0、IOS_月薪10k以上知识大总结


1、UIButton的titleLabel是readOnly属性的。

      UIButton *btn = [[UIButton alloc] init];

      btn.frame = CGRectMake(0, 0, 100, 100);

    1) btn.titleLabel.text = @"Halo";     // 错误

        这种方式会改变当前的text,但一旦状态改变时(如tap一下)则会恢复默认文字

    2)[btn setTitle:@"Halo" forState:UIControlStateNormal];

          [btn setTitle:@"Hello" forState:UIControlStateSelected];  // 正确


2、设置控件不可点击可以通过设置 setUserInteractionEnable 属性为NO。


3、读取NSUserDefaults保存的NSArray,返回值全部immutable(不可修改的)。


4、代码新建的.plist文件要 写内容进去 才会在沙盒中 显示。


5、id类型不允许点语法

    (id)sender;

      sender.tag;  // 错误

      [sender tag];  //get方法,正确


6、iOS全局变量的声明和调用:

      在__AppDelegate中声明:

      @property (nonantic, retain) NSString *g_Name;

      在任意.m文件中调用:

       1) 添加头文件:#import "__AppDelegate.h"

       2) 在需要调用的地方:

           __AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

           appDelegate.g_Name = @"123";


7、iOS开发中经常需要打印CGRect信息:

   NSLog(@"%@"NSStringFromCGRect(pickerView.frame));

       或者   CFShow(NSStringFromCGRect(pickerView.frame));

       打印CGSize信息:

       NSLog(@"%@",NSStringFromCGSize(someCG Size ));


附NSLog的格式如下所示:

  • %@               对象
  • %d, %i           整数 
  • %hi                短整型
  • %lli               长整型
  • %u                无符整形
  • %f                 浮点/双字
  • %0.2f            精度浮点数,且只保留两位小数
  • %x, %X        二进制整数
  • %o                八进制整数
  • %zu              size_t
  • %p               指针
  • %e               浮点/双字 (科学计算)
  • %g               浮点/双字  科学技术法(用最简短的方式)
  • %s C          (char*)字符串
  • %.*s             Pascal字符串
  • %c               字符
  • %C               unichar
  • %lld             64位长整数(long long), 如BOOL值打印
  • %llu             无符64位长整数
  • %Lf              64位双字
  • %lu               sizeof(i)内存中所占字节数

8、新建  empty —>  aViewController.xib 文件时,要在aViewController.xib 的File's Owner中设置Custom Class为aViewController,然后拖动一个UIView控件进来,在referencing outlet中连接 view 和 File's Owner.


9、在做滑动手势识别时,只要判断手势向右就pop出当前View出错:“nested pop animation can result in corrupted navigation bar

 Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.”

解决方法:  判断手势结束之后再pop

- (void)pan:(UIPanGestureRecognizer*)recognizer

{

    CGPoint point = [recognizer translationInView:self.view];

    // 向右滑动

    if (point.x >0)

    {

        // 滑动手势结束后pop出栈,否则出现"nested pop animation can result in corrupted navigation bar"

        if (recognizer.state ==UIGestureRecognizerStateEnded) {

            self.navigationController.interactivePopGestureRecognizer.enabled = YES;

            [self.navigationControllerpopViewControllerAnimated:YES];

        }

    }

}


10、Q:xib文件国际化时如何加载?

        A:xib文件localization之后文件名是一样的,只是存在的lproj目录不一样,可以判断系统当前语言环境加载国家化之后的xib。

    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        // 根据当前系统语言环境,  加载国际化文件
        NSArray *languages = [NSLocale preferredLanguages];
        NSString *currentLanguage = [languages objectAtIndex:0];
        NSLog(@"Current iOS Language is
%@",currentLanguage);
       
        // 获取Bundle里面的xib文件
        NSString *lproj_Path = [[NSBundle mainBundle] pathForResource:currentLanguage ofType:@"lproj" ];
        NSBundle *lproj_Bundle = [NSBundle bundleWithPath:lproj_Path];
        self.viewController = [[ipjsuaViewController alloc] initWithNibName:@"**ViewController_iPhone" bundle:lproj_Bundle];
    } else {
        self.viewController = [[ipjsuaViewController alloc] initWithNibName:@"**ViewController_iPad" bundle:nil];
    }


11、在xib文件国际化时出现"Could not load the '***.png' image referenced from a nib in the bundle with identifier 'null' ",解决方法是将xib中的图片localization(国际化)。


12、给ImageView增加手势UIGestureRecognizer时,发觉无效果。设置UIImageView.userInteractionEnabled = YES后生效。


13、UIButton如果不做设置的话,点击之后会默认变成蓝色。设置tintColor可去掉这种效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值