1。软键盘点击done后消失以及点击next后跳转到下一个textfield
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField ==
name1)
{
[name2becomeFirstResponder];
}
return YES;
}
2。数据永久化储存之NSUserDefaults用法
[[NSUserDefaults standardUserDefaults] setObject:@"hahaah" forKey:@"sam"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSString *value = [[NSUserDefaultsstandardUserDefaults]objectForKey:@"sam"];
NSLog(@"value = %@",value);
3.使用文字的button
btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
btn.frame =CGRectMake(0,0,100,40);
[btnsetTitle:@"btn_off"forState:UIControlStateNormal];
[btnsetTitle:@"btn_on"forState:UIControlStateHighlighted];
[btnaddTarget:selfaction:@selector(onbuttonclick)forControlEvents:UIControlEventTouchUpInside];
4.返回int类型的当前时间和使用int类时间构造date
/////获取系统时间
NSTimeInterval time = [[NSDatedate]timeIntervalSince1970];
NSLog(@"time = %d",(int)time);
NSDate *da = [NSDatedateWithTimeIntervalSince1970 int)time];
NSDateFormatter* formatter = [[NSDateFormatteralloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
NSString* str = [formatter
stringFromDate:da];
NSLog(str);
5。拉伸图片,类似android的.9.png
-(UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
注意5.0以后该接口就deprecated了,需要用另外一个接口,在ios开发文档里有说明
6 往模拟器中添加文件来帮助调试
打开“终端”,依次输入下面的两条命令:
defaults write com.apple.finder AppleShowAllFiles -bool true
KillAll Finder
可以在finder中显示隐藏文件夹
之后找到路径 /Users/<youruser>/Library/Application Support/iPhone Simulator/4.3.2/Applications/<yourappguid>/Documents往里面拷贝文件即可
如果要取消显示隐藏文件夹 依次输入
defaults write com.apple.finder AppleShowAllFiles -bool false
KillAll Finder
即可
7 xcode编译时报错找不到库文件原因
原因是当把刚添加的库文件移到framework分类时,在buildphase里库文件变红了,也就是找不到了,需要重新添加一遍
8.文件操作
NSFileManager *filemanager = [NSFileManager
defaultManager];
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)
objectAtIndex:0];
NSData *data1 = [@"hellosam"
dataUsingEncoding:NSUTF8StringEncoding];
[data1 writeToFile:[document
stringByAppendingPathComponent:@"1.json"]
atomically:YES];
[data1 writeToFile:[document
stringByAppendingPathComponent:@"2.json"]
atomically:YES];
[filemanager createDirectoryAtPath:[document
stringByAppendingPathComponent:@"11"]
withIntermediateDirectories:YES
attributes:nil
error:nil];
BOOL isdir = NO;
BOOL isExist = NO;
NSArray *array = [filemanager
contentsOfDirectoryAtPath:document error:nil];
for(NSString *pathComponent
in array)
{
NSString *path = [document
stringByAppendingPathComponent:pathComponent];
isExist = [filemanager fileExistsAtPath:path
isDirectory:&isdir];
NSLog(@"文件(夹)地址 = %@",path);
NSLog(@"是否存在 = %d,
是否是文件夹 = %d",isExist,isdir);
}
9.获取资源文件
NSString *filepath = [[[NSBundlemainBundle]bundlePath]stringByAppendingPathComponent:@"20120817_142428.3gp"];
10.loadview时报错
在loadview中加载UI时记得调用[super loadview];否则报错
11.ios无法加载资源文件
我在Resources资源文件下放了一个test.vsh得文件,运行后发现无论如何无法在程序内部获取到资源文件,查找原因发现是因为没有加载到应用里,解决方法是在target->build phases->copy bundle resources选项里将这个文件加上就行
12.ios输入import时无法正确提示头文件
一般情况下输入import 后会自动提示需要导入得头文件,但是我明明把文件加入工程了却弹不出来,后来发现是类文件放在了工程顶层目录导致。
也就是说假如我新建一个工程aaaaaa,那么会生成一个aaaaaa目录,里面有个同样叫aaaaaa得目录和一个aaaaaa.xcodeproj得文件,将类文件放入到aaaaaa->aaaaaa文件夹里面即可解决问题,资源文件似乎不受此限
13.ios全屏幕显示照片
很简单,在viewcontroller的init函数里使用
self.wantsFullScreenLayout =YES;即可,此时坐标0,0位于屏幕左上角,被状态栏覆盖,而状态栏成半透明
如果想隐藏状态栏,在任何地方调用
[[UIApplicationsharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];即可
14.icon图标取消高光
在xcode中打开 yourprojectname-info.plist ,在root项上添加Icon already includes gloss and bevel effects项,并将该项选为true,
15.使用NSUserDefaults时,最好使用[userdefault
synchronize];使其立即生效
16.显示和隐藏所有文件夹
显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles YES
隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles NO
输完单击Enter键,退出终端,重新启动Finder就可以了
重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder-->重新启动
17.共享ios的document文件夹
Applicatio-Info.plist内加入一个新的属性UIFileSharingEnabled,然后值设置为true即可
18.获取毫秒级系统时间
struct timeval curtime;
gettimeofday(&curtime,
nil);
long timeM = curtime.tv_sec +curtime.tv_usec/1000000;
NSLog(@"get time by c = %ld",timeM);
NSDate *date = [NSDatedateWithTimeIntervalSince1970:timeM];
19获取系统版本号
[[UIDevicecurrentDevice].systemVersionfloatValue]
20.设置程序每次从后台进入时重新启动
在 info-plist 里面找到 Application does not run in background 一项,勾选即可。
21.获取年月日时间和设置年月日时间
获取当前年月日时间
NSCalendar *calendar = [NSCalendarcurrentCalendar];
NSDateComponents *comps= [calendarcomponents NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit)fromDate:[NSDatedate]];
NSLog(@"year=%d,month=%d,day = %d",[compsyear],[comps
month],[comps day]);
获取时间差
-(int)getDayDurationFromYear:(int)year month:(int)month day:(int)day
{
NSDateComponents *comp = [[NSDateComponentsalloc]init];
[comp setYear:year];
[comp setMonth:month];
[comp setDay:day];
NSCalendar *calendar = [NSCalendarcurrentCalendar];
NSDate *date = [calendar
dateFromComponents:comp];
NSTimeInterval timeinminus = [[NSDatedate] timeIntervalSince1970] - [date timeIntervalSince1970];
int dayinterval = (timeinminus/60.0/60.0/24.0);
[comp release];
return dayinterval;
}
22.uilabel自动调整高度
///自动调整高度
UILabel *label = [[UILabelalloc]
init];
label.frame = CGRectMake(0,120,
200, 20);
label.backgroundColor = [UIColorblueColor];
label.textColor = [UIColorwhiteColor];
label.text =@"sfuiwhifguiweuy2738y8f9hfihsiduhfui32y8yf89ef9g2089ysfhuishfuihuh23ufhuhifgsidgfygig2u3ifhiusgfisuufusgfui";
label.numberOfLines =
0;
[label sizeToFit];
[self.view
addSubview:label];