杂记
1.TextField
for(int j = 0 ;j < n;j ++)
{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(293, 15 + j * 50, 22, 22)];
btn.tag = 100 + j;
[btn setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
if (btn.tag == 100)
{
[btn setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}
[btn addTarget:self action:@selector(checkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[payTypeView addSubview:btn];
}
//方法
-(void)checkBtnClick:(UIButton *)sender
{
NSLog(@"click");
UIButton *button = (UIButton *)sender;
int j = 5;
for (int i = 0; i < j; i++) {
if (i != sender.tag - 100) {
UIButton *button = (UIButton *)[self.view viewWithTag:i + 100];
[button setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
}
else
{
[button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}
}
}
3.倒计时
__block int timeout=30; //倒计时时间
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout<=0){ //倒计时结束,关闭
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置
[l_timeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
l_timeButton.userInteractionEnabled = YES;
});
}else{
// int minutes = timeout / 60;
int seconds = timeout % 60;
NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置
NSLog(@"____%@",strTime);
[l_timeButton setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
l_timeButton.userInteractionEnabled = NO;
});
timeout--;
}
});
dispatch_resume(_timer);
4.字符串转成日期形式
NSString* string = @"20110826134106";
NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSDate* inputDate = [inputFormatter dateFromString:string];
NSLog(@"date = %@", inputDate);
NSDateFormatter *outputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[outputFormatter setLocale:[NSLocale currentLocale]];
[outputFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
NSString *str = [outputFormatter stringFromDate:inputDate];
NSLog(@"testDate:%@", str);
两次打印的结果为:
date = 2011-08-26 05:41:06 +0000
testDate:2011年08月26日 13时41分06秒
说明:上面的时间是美国时间,下面的没有设置
NSString* string = @"Wed, 05 May 2011 10:50:00 +0800";
NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[inputFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate* inputDate = [inputFormatter dateFromString:string];
NSLog(@"date = %@", inputDate);
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy
if (DEVICE_IS_IOS7&&[self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)])
{
self.automaticallyAdjustsScrollViewInsets = NO;
}
6.应用跳转到AppStore评分
1:导入StoreKit.framework,在需要跳转的控制器里面添加头文件#import
2:实现代理SKStoreProductViewControllerDelegate
3:- (void)evaluate{
//初始化控制器
SKStoreProductViewController *storeProductViewContorller = [[SKStoreProductViewController alloc] init];
//设置代理请求为当前控制器本身
storeProductViewContorller.delegate = self;
//加载一个新的视图展示
[storeProductViewContorller loadProductWithParameters:
//appId唯一的
@{SKStoreProductParameterITunesItemIdentifier : @"587767923"} completionBlock:^(BOOL result, NSError *error) {
//block回调
if(error){
NSLog(@"error %@ with userInfo %@",error,[error userInfo]);
}else{
//模态弹出appstore
[self presentViewController:storeProductViewContorller animated:YES completion:^{
}
];
}
}];
}
//取消按钮监听
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- (NSString *)encodeToPercentEscapeString: (NSString *) input
{
NSString *outputStr = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)input,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return outputStr;
}
8.调用地图
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少
if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {//IOS6以下,调用google map
NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",_startCoordinate.latitude,_startCoordinate.longitude,_destinationCoordinate.latitude,_destinationCoordinate.longitude];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *aURL = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:aURL];
}else{
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:_destinationCoordinate addressDictionary:nil]];
toLocation.name = appInstance.currentHotel.hotelAddr;
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}
9.单例书写
+ (InstanceSingleton *)shareAppInstance
{
static InstanceSingleton *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[InstanceSingleton alloc] init];
});
return instance;
}
10.iosGPS定位检测
if ([CLLocationManager locationServicesEnabled] &&
([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized
|| [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined))
11.ios系统中各种设置项的url链接
在代码中调用如下代码:
NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];
[[UIApplication sharedApplication] openURL:url];
即可跳转到设置页面的对应项。
[font=]
About — prefs:root=General&path=About
Accessibility — prefs:root=General&path=ACCESSIBILITY
Airplane Mode On — prefs:root=AIRPLANE_MODE
Auto-Lock — prefs:root=General&path=AUTOLOCK
Brightness — prefs:root=Brightness
Bluetooth — prefs:root=General&path=Bluetooth
Date & Time — prefs:root=General&path=DATE_AND_TIME
FaceTime — prefs:root=FACETIME
General — prefs:root=General
Keyboard — prefs:root=General&path=Keyboard
iCloud — prefs:root=CASTLE
iCloud Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP
International — prefs:root=General&path=INTERNATIONAL
Location Services — prefs:root=LOCATION_SERVICES
Music — prefs:root=MUSIC
Music Equalizer — prefs:root=MUSIC&path=EQ
Music Volume Limit — prefs:root=MUSIC&path=VolumeLimit
Network — prefs:root=General&path=Network
Nike + iPod — prefs:root=NIKE_PLUS_IPOD
Notes — prefs:root=NOTES
Notification — prefs:root=NOTIFICATIONS_ID
Phone — prefs:root=Phone
Photos — prefs:root=Photos
Profile — prefs:root=General&path=ManagedConfigurationList
Reset — prefs:root=General&path=Reset
Safari — prefs:root=Safari
Siri — prefs:root=General&path=Assistant
Sounds — prefs:root=Sounds
Software Update — prefs:root=General&path=SOFTWARE_UPDATE_LINK
Store — prefs:root=STORE
Twitter — prefs:root=TWITTER
Usage — prefs:root=General&path=USAGE
VPN — prefs:root=General&path=Network/VPN
Wallpaper — prefs:root=Wallpaper
Wi-Fi — prefs:root=WIFI

浙公网安备 33010602011771号