杂记

1.TextField

当无输入内容时,底部按钮背景为灰色;当有输入,按钮变色;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *toBEString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([toBEString length] == 0) {
        [affirmBut setBackgroundImage:[UIImage imageNamed:@"button_gray_02.png"] forState:UIControlStateNormal];
    }else{
        [affirmBut setBackgroundImage:[UIImage imageNamed:@"button_orange_02.png"] forState:UIControlStateNormal];
    }
    
    return YES;
}
 
2.单选框
n = 5(可以自定义);

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.字符串转成日期形式

1、如何如何将一个字符串如“ 20110826134106”装化为任意的日期时间格式,下面列举两种类型:
   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 
 
5.引导页中有偏移

if (DEVICE_IS_IOS7&&[self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)])

    {

        self.automaticallyAdjustsScrollViewInsets = NO;

    }

6.应用跳转到AppStore评分

在ios6.0,APPle增加了一个心得功能,当用户需要给APP评分时候,不再跳转到appstore了,可以在应用内实现打开appstore,苹果提供了一个框架StoreKit.framework,实现步骤如下:
  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:^{
       
    }];
}
7.url编码

- (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

 

 

 

 

 

 

posted @ 2015-01-07 14:16  TinyGirl  Views(121)  Comments(0)    收藏  举报