2014년 11월 20일 목요일

keyboard 에서 enter 키 누르면 키보드 숨기기


UITextField 의 경우

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"]) {
        [self.view endEditing:YES];
    }
    return YES;
}



UITextView 의 경우


#pragma mark - UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [self.view endEditing:YES];
    }
    return YES;

}

2014년 11월 3일 월요일

최상위 View Controller 가져오기



+ (UIViewController*)topMostViewController {
    UIViewController *topMostViewController = nil;
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        topMostViewController = navigationController.visibleViewController;
    } else if (rootViewController.presentedViewController) {
        topMostViewController = rootViewController.presentedViewController;
    } else if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        topMostViewController = tabBarController.selectedViewController;
    } else
        topMostViewController = rootViewController;
    
    return topMostViewController;

}

2014년 10월 15일 수요일

#define

#define IS_IPAD    (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

#define POINT(_INDEX_) [(NSValue *)[points objectAtIndex:_INDEX_] CGPointValue]

#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:r/255.f green:g/255.f blue:b/255.f alpha:a]

#define ColorWithHexa(rgbValue) [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 \
                                        green:((rgbValue & 0xFF00) >> 8)/255.0 \

                                        blue:(rgbValue & 0xFF)/255.0 alpha:1.0]; \

#define RESIZABLE(_VIEW_) [_VIEW_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth]

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))

// check iOS version
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)


#define DOCUMENTS_DIR ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject])
#define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SAFE_RELEASE(obj) ([obj release], obj = nil)
#define SAFE_TIMER_RELEASE(obj) ([obj invalidate]; [obj release]; obj = nil)
#define SAFE_ASSIGN(obj, expr) ([(expr) retain], [obj release], obj = (expr))

2014년 10월 9일 목요일

@ 매크로


1.
    NSString *string = @"<#string#>"

2.  
    id express = @(<#expression#>)

  (ex)
    NSInteger idx = 40;
    NSString *idxString = [@(idx) stringValue];

3.    
    NSArray *array =  @[<#objects, ...#>]

4.    
    @autoreleasepool {
        <#statements#>
    }

5.    
    char[] chars =  @encode(<#type-name#>)

6.    
    Protocol *protocol = @protocol(<#protocol-name#>)

7.    
    SEL sel = @selector(<#selector#>)

8.    
    @throw <#expression#>
    
9.
    NSDictonary *dic = @{<#key#>: <#object, ...#>}
    
10.
    @try {
        <#Code that can potentially throw an exception#>
    }
    @catch (NSException *exception) {
        <#Handle an exception thrown in the @try block#>
    }
    @finally {
        <#Code that gets executed whether or not an exception is thrown#>

    }

11. structure

    CGSize theSize = {
        .width = 100.f,
        .height = 100.f,
    };
    
    CGRect theRect = {
        {.x = 10.f, .y= 10.f},
        {.width = 200.f, .height = 200.f}
    };

2014년 10월 1일 수요일

UIAlertView & Block function

source

//
//  CustomUIAlertView.m
//  patternpie
//
//  Created by MYUNG GU KIM on 10/2/14.
//  Copyright (c) 2014 piestudio. All rights reserved.
//

#import "CustomUIAlertView.h"

@implementation CustomUIAlertView

- (void)showWithBlock:(void (^)(int selectedIndex))complete
{
    self.delegate = self;
    ptrComplete = complete;
    [self show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    ptrComplete((int)buttonIndex);
}


@end




header 

//
//  CustomUIAlertView.h
//  patternpie
//
//  Created by MYUNG GU KIM on 10/2/14.
//  Copyright (c) 2014 piestudio. All rights reserved.
//

#import <UIKit/UIKit.h>

void (^ptrComplete)(int);

@interface CustomUIAlertView : UIAlertView <UIAlertViewDelegate>
{
}
- ( void )showWithBlock: ( void ( ^ )( int selectedIndex) )complete;
@end





using


[[[CustomUIAlertView alloc] initWithTitle:nil message:@"Do you want Challenge?" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil] showWithBlock:^(int selectedIndex) {
            NSLog(@"You selected button %d", selectedIndex);
        }];




2014년 9월 30일 화요일

random


1.  0~10 까지의 랜덤 숫자

uint32_t random = arc4random_uniform(11);

2.  50~100 까지의 랜덤 숫자

uint32_t random = arc4random_uniform(51)+50;




3.  랜덤 숫자

unsigned int random = arc4random();


4. 컬러값 랜덤으로 가져오기

static func randomBackgroundColor() -> UIColor {
        let colors = [UIColor.rgb(5, 200, 123), UIColor.rgb(176, 158, 145), UIColor.rgb(106, 97, 248), UIColor.rgb(33, 173, 254), UIColor.rgb(249, 135, 40)]
        let randomIndex = Int(arc4random_uniform(UInt32(colors.count)))
        return colors[randomIndex]

    }

2014년 9월 23일 화요일

시, 분, 초 계산 방법


   CFTimeInterval _ticks;




    _ticks += 0.1;
    double seconds = fmod(_ticks, 60.0);
    double minutes = fmod(trunc(_ticks / 60.0), 60.0);
    double hours = trunc(_ticks / 3600.0);
    self.timerLabel.text = [NSString stringWithFormat:@"%02.0f:%02.0f:%04.1f", hours, minutes, seconds];
    

    NSLog(@"tick = %f text = %@", _ticks, self.timerLabel.text);





2014-09-24 02:59:49.241 TabbedBanner[961:60b] tick = 0.100000 text = 00:00:00.1
2014-09-24 02:59:49.341 TabbedBanner[961:60b] tick = 0.200000 text = 00:00:00.2
2014-09-24 02:59:49.441 TabbedBanner[961:60b] tick = 0.300000 text = 00:00:00.3
...
2014-09-24 02:59:53.141 TabbedBanner[961:60b] tick = 4.000000 text = 00:00:04.0
2014-09-24 02:59:53.241 TabbedBanner[961:60b] tick = 4.100000 text = 00:00:04.1
2014-09-24 02:59:53.341 TabbedBanner[961:60b] tick = 4.200000 text = 00:00:04.2
2014-09-24 02:59:53.441 TabbedBanner[961:60b] tick = 4.300000 text = 00:00:04.3
...
2014-09-24 03:03:35.186 TabbedBanner[961:60b] tick = 224.900000 text = 00:03:44.9
2014-09-24 03:03:35.286 TabbedBanner[961:60b] tick = 225.000000 text = 00:03:45.0
2014-09-24 03:03:35.386 TabbedBanner[961:60b] tick = 225.100000 text = 00:03:45.1
2014-09-24 03:03:35.486 TabbedBanner[961:60b] tick = 225.200000 text = 00:03:45.2
2014-09-24 03:03:35.586 TabbedBanner[961:60b] tick = 225.300000 text = 00:03:45.3
2014-09-24 03:03:35.686 TabbedBanner[961:60b] tick = 225.400000 text = 00:03:45.4