您的位置:首页 > 移动开发 > IOS开发

iOS_开发中遇到的那些问题_5

2015-12-14 22:03 609 查看
【自编号:540】【iOS是否锁屏】
设置UIApplication的idleTimerDisabled属性来指定iOS是否锁频:
// 禁用休闲时钟
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];

// 也可以使用这种语法
[UIApplication sharedApplication].idleTimerDisabled = YES;
【2015-08-19 09:59:32 by beyond】【自编号:541】【iOS的锁屏和Home事件】
得到iOS的锁屏和Home事件,并分布做出响应。
在iOS7之前通过[[UIApplication sharedApplication] applicationState]可以分别获取到两个的状态, 锁屏:UIApplicationStateInactive Home事件:UIApplicationStateBackground。 这样就可以很方便的区分两个事件了。
但是到了iOS7,这个方法就行不通了。因为两个的事件的状态都变成了UIApplicationStateBackground。
通过一番搜索,在stackoverflow上找到了答案: iOS7得到UIApplicationStateBackground状态后,通过区分屏幕的亮度来区分Home与锁屏事件。
当屏幕亮度为0时,为锁屏,否则为Home事件:
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateInactive) {
//iOS6锁屏事件
NSLog(@"Sent to background by locking screen");
} else if (state == UIApplicationStateBackground) {
CGFloat screenBrightness = [[UIScreen mainScreen] brightness];
NSLog(@"Screen brightness: %f", screenBrightness);
if (screenBrightness > 0.0) {
//iOS6&iOS7 Home事件
NSLog(@"Sent to background by home button/switching to other app");
} else {
//iOS7锁屏事件
NSLog(@"Sent to background by locking screen");
}
}
【2015-08-19 10:12:01 by beyond】【自编号:542】【神奇的IB_DESIGNABLE和IBInspectable】【查看原文】 【2015-08-21 11:06:08 by beyond】【自编号:543】【神奇的IB_DESIGNABLE和IBInspectable】【查看原文】 【2015-08-21 11:06:08 by beyond】【自编号:544】【使用Label作约束控制的蒙板时,一定勾选user interaction enabled,不然后面的输入框或按钮会接收点击事件】【2015-08-21 17:48:51 by beyond】【自编号:551】【图片拉伸】
//        // 对右图片进行拉伸
//
CGFloat top = 0; // 顶端盖高度
CGFloat bottom = 0 ; // 底端盖高度
CGFloat left = 1; // 左端盖宽度
CGFloat right = image.size.width -1 -1; // 右端盖宽度
// 实际拉伸的是 全高 X 1个点宽
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
//        // 伸缩后重新赋值
//        image = [image resizableImageWithCapInsets:insets];

// 实际拉伸的是:只有一个点
image = [image stretchableImageWithLeftCapWidth:1 topCapHeight:1];
【查看原文】 【2015-08-25 15:00:18 by beyond】【自编号:552】【NSDatePicker】【查看原文】 【2015-08-25 18:32:58 by beyond】【自编号:553】【linker flag】
编译正常,但运行时,找不到分类里面的方法
使用下面两个 编译参数均可(推荐后一个)
-all_load
-ObjC
【2015-08-25 23:04:29 by beyond】【自编号:555】【应用程序本地化】【查看原文】 【2015-08-26 15:00:52 by beyond】【自编号:559】【XCode快速打开 (Cmd-Shift-O)】
快速打开
使用快速打开 (Cmd-Shift-O) 功能,可快速访问项目使用的任何文件。Xcode 即刻提供搜索的实现方式,您可以选择一个方式,然后点击 Return 来打开文件,或点击 Option-Return 在 Assistant 编辑器中打开文件。
【2015-08-28 10:25:06 by beyond】【自编号:560】【XCode快速打开 (Cmd-Shift-O)】
快速打开
使用快速打开 (Cmd-Shift-O) 功能,可快速访问项目使用的任何文件。Xcode 即刻提供搜索的实现方式,您可以选择一个方式,然后点击 Return 来打开文件,或点击 Option-Return 在 Assistant 编辑器中打开文件。
【2015-08-28 10:25:06 by beyond】【自编号:562】【日期天数间隔】
- (int)tool_daySpanFromDate1:(NSDate *)d1 toDate2:(NSDate *)d2
{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSChineseCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit fromDate:d1 toDate:d2 options:0]; // NSCalendarUnitDay
NSInteger days = [components day];
return days;
}
【2015-08-28 15:05:47 by beyond】【自编号:563】【身份证取值 】
例子:
650105199111126546
女
1991.11
24
管雪曼

根据身份证号码(15位和18位通用)自动提取性别和出生年月的自编公式,供需要的网友参考:
说明:公式中的B2是身份证号
1、根据身份证号码求性别:
=if(len(B2)=15,if(mod(value(right(B2,3)),2)=0,"女","男"),if(len(B2)=18,if(mod(value(mid(B2,15,3)),2)=0,"女","男"),"身份证错"))
2、根据身份证号码求出生年月:
=if(len(B2)=15,CONCATENATE("19",mid(B2,7,2),".",mid(B2,9,2)),IF(LEN(B2)=18,CONCATENATE(MID(B2,7,4),".",MID(B2,11,2)),"身份证错"))

3、根据身份证号码求年龄:
=IF(LEN(B2)=15,year(now())-1900-VALUE(MID(B2,7,2)),if(LEN(B2)=18,year(now())-VALUE(MID(B2,7,4)),"身份证错"))
【2015-08-28 17:51:35 by beyond】【自编号:564】【身份证详解】
身份证号码各位数字的含义如下:
1)前1、2位数字表示:所在省份的代码;
(2)第3、4位数字表示:所在城市的代码;
(3)第5、6位数字表示:所在区县的代码;
(4)第7~14位数字表示:出生年、月、日;
(5)第15、16位数字表示:所在地的派出所的代码;
(6)第17位数字表示性别:奇数表示男性,偶数表示女性;
(7)第18位数字是校检码:也有的说是个人信息码,一般是随计算机的随机产生,
用来检验身份证的正确性。校检码可以是0~9的数字,有时也用x表示。
【2015-08-28 18:03:46 by beyond】【自编号:565】【15位身份证号码】
15位身份证号码各位的含义:
1-2位省、自治区、直辖市代码;
3-4位地级市、盟、自治州代码;
5-6位县、县级市、区代码;
7-12位出生年月日,比如670401代表1967年4月1日,与18位的第一个区别;
13-15位为顺序号,其中15位男为单数,女为双数;
与18位身份证号的第二个区别:没有最后一位的验证码。
举例:
130503 670401 001的含义; 13为河北,05为邢台,03为桥西区,出
生日期为1967年4月1日,顺序号为001。
【2015-08-28 18:06:19 by beyond】【自编号:567】【JSONModel源码笔记】【查看原文】 【2015-08-31 14:18:49 by beyond】【自编号:568】【CFUUIDRef 和CFStringRef来生成一个唯一的标识】
NSString *prefix = @"Beyond";
NSString *  result;
CFUUIDRef   uuid;
CFStringRef uuidStr;
uuid = CFUUIDCreate(NULL);
uuidStr = CFUUIDCreateString(NULL, uuid);
result =[NSString stringWithFormat:@"%@-%@", prefix,uuidStr];
CFRelease(uuidStr);
CFRelease(uuid);
NSLog(@"%@",result);
// Beyond-B370EE87-2C60-4C3B-A99F-418604DC3B67
// Beyond-510E0DCD-C265-47D6-8BEB-50022449E062

或者这样也行:
+ (NSString *)UUIDString
{
CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);

CFRelease(uuid_ref);
NSString *uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref];

CFRelease(uuid_string_ref);
return uuid;
}
【2015-08-31 14:30:54 by beyond】【自编号:569】【导入头文件CommonDigest进行MD5加密】
#import
NSString *input = @"123456";
const char *cStr = [input UTF8String];
unsigned char digest[16];
CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call

NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"x", digest[i]];
NSLog(@"md5加密后:%@",output);
//        e10adc3949ba59abbe56e057f20f883e
【2015-08-31 14:38:33 by beyond】【自编号:570】【ipa】【进入网址】 【2015-08-31 16:49:16 by beyond】【自编号:571】【内存与地址】
NSString *str = @"beyond"; // 常量内存区的地址,只要值相同,占用的地址空间是一致的
NSString *str2 = @"beyond";
NSLog(@"%p", str); //  0x100001048
NSLog(@"%p", str2); // 0x100001048
NSLog(@"%lu", sizeof(str)); // 32位中 str占4个字节;64位中 str占8个字节
【查看原文】 【2015-08-31 17:54:46 by beyond】【自编号:572】【iOS判断一个字符串中是否都是数字】
iOS判断一个字符串中是否都是数字
时间:2014-12-05 15:08  来源:未知  阅读次数: 830 复制分享 我要评论
第一种方式是使用NSScanner:

1. 整形判断

- (BOOL)isPureInt:(NSString *)string{

NSScanner* scan = [NSScanner scannerWithString:string];

int val;

return [scan scanInt:&val] && [scan isAtEnd];

}

2.浮点形判断:

- (BOOL)isPureFloat:(NSString *)string{

NSScanner* scan = [NSScanner scannerWithString:string];

float val;

return [scan scanFloat:&val] && [scan isAtEnd];

}

第二种方式是使用循环判断

- (BOOL)isPureNumandCharacters:(NSString *)text
{
for(int i = 0; i < [text length]; ++i) {

int a = [text characterAtIndex:i];
if ([self isNum:a]){

continue;
} else {
return NO;
}
}
return YES;
}

或者 C语言中常用的方式.

- (BOOL)isAllNum:(NSString *)string{
unichar c;
for (int i=0; i 0)
{

return NO;

}
return YES;

}

以上三种能够帮助实现判断是否为数字的函数,iOS中没有直接判断是否是数字的方法,所以只能够自己添加方法去实现了.
【2015-08-31 17:55:32 by beyond】【自编号:574】【NSPredicate evaluateWithObject】
NSString *telNo = @"13469434404";
BOOL isPhone = NO;
NSString *regexStr = @"^1((3[0-9])|(4[57])|(5[0-35-9])|(7[06-8])|(8[0-9])|70)\\d{8}$";

// Creates and returns a new predicate formed by creating a new string with a given format and parsing the result.
NSPredicate *phonePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexStr];

// Returns a Boolean value that indicates whether 【a given object】 matches the conditions specified by the receiver.
isPhone = [phonePredicate evaluateWithObject:telNo];
【2015-09-01 15:24:31 by beyond】【自编号:575】【使用分类为系统控件动态添加@dynamic属性】
@dynamic经常在NSManagedObject的子类中的使用

@dynamic最常用的使用是在NSManagedObject中,此时不需要显示编程setter和getter方法。原因是:@dynamic告诉编译器不做处理,使编译通过,其getter和setter方法会在运行时动态创建,由Core Data框架为此类属性生成存取方法。

//
//  UIButton+PhoneNumber.h
//
//  Created by beyond on 15-9-1.
//  Copyright (c) 2015年 sg31.com. All rights reserved.
//

#import

@interface UIButton (PhoneNumber)
@property (nonatomic, strong) NSString * phoneNumber;
@property (nonatomic, assign) NSInteger  btnType;//0:即时聊天;1:免费通话;2:邀请;3:短信
@end

@interface UILabel (PhoneNumber)
@property (nonatomic, strong) NSString * phoneNumber;
@end

//
//  UIButton+PhoneNumber.h
//
//  Created by beyond on 15-9-1.
//  Copyright (c) 2015年 sg31.com. All rights reserved.
//

#import "UIButton+PhoneNumber.h"
#import

@implementation UIButton (PhoneNumber)
// 动态属性
@dynamic phoneNumber;

- (void)setPhoneNumber:(NSString *)phoneNumber
{
objc_setAssociatedObject(self, @selector(phoneNumber), phoneNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)phoneNumber
{
return objc_getAssociatedObject(self, @selector(phoneNumber));
}

- (void)setBtnType:(NSInteger)btnType
{
objc_setAssociatedObject(self, @selector(btnType), [NSNumber numberWithInteger:btnType], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSInteger)btnType
{
return [objc_getAssociatedObject(self, @selector(btnType)) integerValue];
}

@end

@implementation UILabel (PhoneNumber)
// 动态属性
@dynamic phoneNumber;

- (void)setPhoneNumber:(NSString *)phoneNumber
{
objc_setAssociatedObject(self, @selector(phoneNumber), phoneNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)phoneNumber
{
return objc_getAssociatedObject(self, @selector(phoneNumber));
}

@end
【2015-09-01 15:37:55 by beyond】【自编号:577】【本地化按首字母分组排序的神器—UILocalizedIndexedCollation】
首先提一下,UILocalizedIndexedCollation的分组排序是建立在对对象的操作上的。
例如:Person类:

@interface Person : NSObject
@property(nonatomic, copy) NSString *name;
@end
然后初始化一些对象存入一个数组(注:为了后续说明方便,我直接拿name的值来表示Person类的对象,实际编码中是要用对象!如下列<林妹妹>表示p.name = @"林妹妹"的Person类对象p)

1 NSArray *srcArray = @[<林黛玉>, <林妹妹>, <周董>, <周树人>, <周杰伦>, <阿华>];
先将UILocalizedIndexedCollation初始化,

1 UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
可以看出来这是一个单例,它会根据不同国家的语言初始化出不同的结果。如中文和英文的得到的就是A~Z和#,日语的就是A~Z,あ, か, さ, た, な, は, ま, や, ら, わ和#。下边我就以最熟悉的中文环境为例,直接上代码了,注意看注释部分的讲解

复制代码
1 //得出collation索引的数量,这里是27个(26个字母和1个#)
2 NSInteger sectionTitlesCount = [[collation sectionTitles] count];
3
4 //初始化一个数组newSectionsArray用来存放最终的数据,我们最终要得到的数据模型应该形如@[@[以A开头的数据数组], @[以B开头的数据数组], @[以C开头的数据数组], ... @[以#(其它)开头的数据数组]]
5
6 NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
7
8 //初始化27个空数组加入newSectionsArray
9 for (NSInteger index = 0; index < sectionTitlesCount; index++) {
10     NSMutableArray *array = [[NSMutableArray alloc] init];
11     [newSectionsArray addObject:array];
12 }
13
14 //将每个人按name分到某个section下
15
16 for (Person *p in srcArray) {
17   //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11
18     NSInteger sectionNumber = [collation sectionForObject:p collationStringSelector:@selector(name)];
19   //把name为“林丹”的p加入newSectionsArray中的第11个数组中去
20     NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
21     [sectionNames addObject:p];
22 }
23
24 //对每个section中的数组按照name属性排序
25 for (NSIntger index = 0; index < sectionTitlesCount; index++) {
26     NSMutableArray *personArrayForSection = newSectionsArray[index];
27     NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
28     newSectionsArray[index] = sortedPersonArrayForSection;
29 }
复制代码
最终把newSectionsArray应该形如@[@[<阿华>], @[], @[], ... @[<林丹>, <林荣>], ... @[<周董>, <周杰伦>, <周树人>], @[]]

后续工作就是把这个数组作为数据源与UITableView通过tableView的Delegate关联起来了,部分如下,在此不再赘述

复制代码
1 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
2     return [collation sectionTitles][section];
3 }
4
5 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
6     return [collation sectionIndexTitles];
7 }
8
9 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
10     return [collation sectionForSectionIndexTitleAtIndex:index];
11 }
复制代码
  不过呢,使用这个UILocalizedIndexedCollation有一个缺点就是不能区分姓氏中的多音字,比如“曾”会被分到"C"组去,不知道大家有没有基于此的好方法在下边回复。下边是苹果官方示例,其中第3个是关于UILocalizedIndexedCollation的,可以下载下来学习一下http://developer.apple.com/library/ios/samplecode/TableViewSuite/Introduction/Intro.html
【2015-09-02 11:42:13 by beyond】【自编号:578】【侧滑删除时,文字更改】
XCode 4.5 下载地址 (iOS 6.0) http://mac.pcbeta.com/thread-109022-1-1.html 
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1) {
return @"设为默认";
}
return NSLocalizedString(@"move_top", nil);
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{}
【2015-09-02 17:20:45 by beyond】【自编号:579】【类似做网页点击放大】
使用CGContextTranslateCTM和CGContextScaleCTM真不容易,数学不好的哥们头的晕,比如我就是

下面的代码是用来类似做网页点击放大的效果,setTouchPoint是通过touchedmove去调用更新touchpoint点,调整放大镜的center的位置,而放大的效果则在下面drawRect里,而核心就在于如何在矩阵中变化,比如平移,旋转,scale缩放

[plain] view plaincopy
- (void)setTouchPoint:(CGPoint)point {
touchPoint = point;
self.center = CGPointMake(point.x + touchPointOffset.x, point.y + touchPointOffset.y);
}

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, self.frame.size.width/2, self.frame.size.height/2 );
CGContextScaleCTM(context, scale, scale);
CGContextTranslateCTM(context, -touchPoint.x, -touchPoint.y + (self.scaleAtTouchPoint? 0 : self.bounds.size.height/2));
[self.viewToMagnify.layer renderInContext:context];
}

显示平移到self的中心点
然后放大矩阵,然后移动坐标使viewTmoMagnify中的touch坐标与self中心点坐标对齐,从而实现放大效果。
【2015-09-02 17:51:07 by beyond】【自编号:581】【调整label内边距】
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insects)];
【2015-09-06 11:36:57 by beyond】【自编号:582】【不同时滚动】
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
CGRect frame = self.topImageView.frame;
CGFloat factor;
if (offset.y < 0) {
// 关键在于判断如果是在table的顶部向下拉动,修改bgImgView的frame使其跟随table一起移动,只不过移动比table慢(到底多慢由你定)
factor = 0.5;
} else {
factor = 1;
}

float y = kTopImageViewPositionY;
frame.origin.y = y - offset.y*factor;
self.topImageView.frame = frame;
}
【2015-09-07 10:41:51 by beyond】【自编号:583】【汇报一个重大错误】
cell的配置数据源方法中:
self.statusLabel.textColor = [self colorWithFSate:model.FState];
- (UIColor *)colorWithFSate:(NSInteger)FState
{
// 最新的民宿订单状态:0全部,1待确认,2确认待支付,3支付待入住,4入住待离店,5结束,6取消
switch (FState) {
case 0:
{
return kColor(200,200,200);//@"全部";
}
break;
case 1:
{
return kColor(40, 40, 40);//@"待确认";
}
break;
case 2:
{
return kColor(0,50,160);//@"待支付";
}
break;
case 3:
{
return kColor(0,170,80);//@"待入住";
}
break;
case 4:
{
return kColor(200,200,50);//@"待离店";
}
break;
case 5:
{
return kColor(200,70,20);//@"已结束";
}
break;
case 6:
{
return kColor(220,35,60);//@"已取消";
}
break;
// case 7:
//{
//  return kColor(220,35,60);//@"这是什么鬼???";
//}
//  break;

default:
break;
}
}

由于服务端临时添加一个7的状态,而客户端的default未返回默认的颜色值,
导致程序运行时遇到7的状态,发生崩溃,并且无法通过全局断点进行定位
XCode报错是:[cell set]方法找不到。。。

最后,只能通过step断点进行手动定位
【2015-09-08 10:33:58 by beyond】【自编号:584】【IBOutlet必须要先删除原来的关联,再添加新的关联,不然会一团糟,原因是两个XIB文件的某些ID不一样了】
【2015-09-08 14:55:01 by beyond】
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 问题 XCODE