以下内容是在开发中总结的一些小知识,如果有错,请各位看官提出,谢谢!
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可去掉这种效果。