Develop
2013.08.08 13:07
[ios] 소소한 팁 (Rect,Point,Path,URL 등)
Views 29530 Comment 0
1. 빌드시 시뮬레이터 판별
2. 하위 View 검색
초기화 하는 곳에서 [self setSubViewsClearColor:self]; 와 같이 호출하여 사용합니다.
3. 사용자 데이터 저장
4. Rect와 Point
좌표로 많이 사용되는 Rectd와 Point에서 자주 사용되는 함수와 상수입니다.
5. Path
6. URL
#if !TARGET_IPHONE_SIMULATOR
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif
TARGET_IPHONE_SIMULATOR로 실제 아이폰에서만 실행되는 코드를 따로 관리할 수 있습니다. pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif
2. 하위 View 검색
NSArray *subViewList = [searchBar subviews];
for (UIView *view in subViewList) {
if ([view isKindOfClass:[UITextField class]]) {
[(UITextField *)view setReturnKeyType:UIReturnKeyDone];
}
}
UIView의 subviews와 isKindOfClass를 사용하여 하위의 특정 뷰를 찾아내어 설정을 변경할 수 있습니다. UISearchBar에서 UITextField를 찾아내어 키보드의 Search 버튼의 텍스트를 Done으로 변경하는 예입니다.for (UIView *view in subViewList) {
if ([view isKindOfClass:[UITextField class]]) {
[(UITextField *)view setReturnKeyType:UIReturnKeyDone];
}
}
- (void) setSubViewsClearColor: (UIView*)theView {
NSArray *subViewList = theView.subviews;
for (UIView *view in subViewList) {
[view setBackgroundColor:[UIColor clearColor]];
[self setSubViewsClearColor:view];
}
}
하위 View를 모두 찾아 배경을 투명한 속성으로 변경하는 예입니다. NSArray *subViewList = theView.subviews;
for (UIView *view in subViewList) {
[view setBackgroundColor:[UIColor clearColor]];
[self setSubViewsClearColor:view];
}
}
초기화 하는 곳에서 [self setSubViewsClearColor:self]; 와 같이 호출하여 사용합니다.
3. 사용자 데이터 저장
userLevel = [[NSUserDefaults standardUserDefaults] integerForKey:@"user_level"];
[[NSUserDefaults standardUserDefaults] setInteger:g_userLevel forKey:@"user_level"];
옵션등의 간단한 설정은 데이터베이스나 파일을 이용하는대신 NSUserDeraults를 사용하면 간단하게 저장하고 불러올 수 있습니다.[[NSUserDefaults standardUserDefaults] setInteger:g_userLevel forKey:@"user_level"];
4. Rect와 Point
좌표로 많이 사용되는 Rectd와 Point에서 자주 사용되는 함수와 상수입니다.
CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);
x, y, width, height로 설정된 CGRect를 반환합니다.CGRectZero
0, 0 좌표와 0, 0 크기를 가진 CGRect 상수입니다.CGPointMake(CGFloat x, CGFloat y);
x, y로 설정된 CGPoint를 반환합니다.CGPointZero
0, 0 좌표를 가진 CGPoint 상수입니다.bool CGRectContainsPoint(CGRect rect, CGPoint point);
rect 사각형에 point가 속해있는지 여부를 반환합니다.bool CGRectContainsRect(CGRect rect1, CGRect rect2);
rect1 사각형에 rect2 사각형이 속해있는지 여부를 반환합니다.bool CGRectIntersectsRect (CGRect rect1, CGRect rect2);
rect1과 rect2가 교차하는지 여부를 반환합니다.5. Path
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
어플리케이션 번들 디렉토리를 반환합니다.NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
번들에 포함된 파일들은 읽기만 가능하고 쓰기가 불가능합니다. 디비와 같이 변경이 필요한 경우에는 위와같이 어플리케이션의 도큐먼트 폴더를 구해와 도큐먼트 폴더로 복사 생성해 놓고 작업합니다.NSString *documentsDirectory = [paths objectAtIndex:0];
6. URL
NSURL *url = [NSURL URLWithString:@"http://www.cocoadev.co.kr"];
[[UIApplication sharedApplication] openURL:url];
지정된 웹주소를 사파리에서 오픈합니다.[[UIApplication sharedApplication] openURL:url];
NSURL *url = [NSURL URLWithString:@"mailto:abc@def.com"];
[[UIApplication sharedApplication] openURL:url];
받는사람이 설정되어 메일 프로그램의 새로운 메시지가 실행됩니다.[[UIApplication sharedApplication] openURL:url];
NSURL *url = [NSURL URLWithString:@"tel:02-111-2222"];
[[UIApplication sharedApplication] openURL:url];
지정된 번호로 전화를 겁니다.[[UIApplication sharedApplication] openURL:url];
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
60 | Develop | [ios] Facebook Cache 갱신하는 함수 | hooni | 2017.02.27 | 1325 |
59 | Develop | [ios] Facebook SDK 로그인 설명 | hooni | 2017.04.19 | 1335 |
58 | Develop | [ios] FlckrFeed Example App (Swift) | hooni | 2016.11.27 | 1203 |
57 | Develop | [ios] iOS 4.0 beta 에서 3.1.3으로 다운그레이드 하는 법 | hooni | 2013.04.23 | 37542 |
56 | Develop | [ios] NavigationController 에서 왼쪽(back) 버튼 후킹하기 | hooni | 2015.10.23 | 1299 |
55 | Develop | [ios] NSString, RegularExpression Find/Replace | hooni | 2017.04.14 | 956 |
54 | Develop | [ios] Random Thoughts: Rand() vs. arc4random() | hooni | 2013.10.31 | 68119 |
53 | Develop | [ios] Requesting Location Permissions in iOS | hooni | 2018.08.18 | 1735 |
52 | Develop | [ios] UIAlertView 초간단 샘플 ㅎㅎ | hooni | 2013.10.14 | 46193 |
51 | Develop | [ios] UIView에서 상위 UIViewController 가져오기 | hooni | 2013.09.27 | 20238 |
50 | Develop | [ios] UUID 생성 + Key Chain 연동 | hooni | 2016.05.13 | 4784 |
49 | Develop | [ios] Xcode에서 특정 파일만 ARC 따로 설정하는 방법 | hooni | 2017.03.29 | 1081 |