Develop
2015.01.03 14:53
[ios] Thread Loop 내에서 UI 업데이트 방법
조회 수 916 댓글 0
IOS SDK에서는 UI 의 접근을 Main Thread에서 만 허용하고 있습니다.
그래 다음과 같은 코드를 실행하면 UI가 업데이트 되지 않는데요.
- (void)thread_function { for (; ;) { textView.text = @"hello"; } } -(void)StartRecvThread { [NSThread detachNewThreadSelector:@selector(thread_function) toTarget:self withObject:nil]; }
textView가 업데이트 되지 않습니다.
이럴 때에는 performSelectorOnMainThread를 이용하여 메인 쓰레드로 접근하는 방법이 있습니다.
[textView performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"Thread count: %d ", count] waitUntilDone:YES];
이런 식으로 사용하면 되는데요.
위의 예제를 수정하면 다음과 같습니다.
- (void)thread_function { for (; ;) { [textView performSelectorOnMainThread:@selector(setText:) withObject:@"hello" waitUntilDone:YES]; } }
한편 timer를 설정하는 접근 하는 방법도 있습니다.
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(processUpdate:) userInfo:nilrepeats:YES]; -(void)processUpdate:(NSTimer *)theTimer { textfield1.text = [NSString stringWithFormat:@"Thread count: %d ", count]; }
이런 식으로 하면 되겠지요.
[출처] http://www.digipine.com/programming/6779/page/2
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
253 | Develop | [ios] 배열(NSArray) 연산과 간단한 애니메이션(split images) | hooni | 2013.10.31 | 45497 |
252 | Develop | [php] XE 에서 php 구문 사용하기 (XE 템플릿에서) | hooni | 2013.10.31 | 19528 |
251 | Develop | [js] 웹페이지에서 특정 엘리먼트 드래그, 복사, 컨텍스트메뉴, 키보드 막기 | hooni | 2013.11.04 | 30121 |
250 | Develop | [python] 파이썬 공부하는 사이트~ | hooni | 2013.11.12 | 10876 |
249 | Develop |
[java] 초간단 싱글톤(Singleton) 패턴 샘플 코드
![]() |
hooni | 2013.11.18 | 11623 |
248 | Develop | [php] php5.3부터는 eregi()대신 preg_match()를 사용 | hooni | 2013.11.18 | 12518 |
247 | Develop | [js] window.open() 속성 사용 방법 | hooni | 2013.11.18 | 13628 |
246 | Develop | [ios] iphone SetDeviceOrientation 화면 강제 회전 | hooni | 2013.11.20 | 18501 |
245 | Develop |
[ios] iOS In App Purchase #1 (코드 구현 전 웹 설정 작업)
![]() |
hooni | 2013.11.20 | 14544 |
244 | Develop |
[ios] iOS In App Purchase #2 (코드 구현)
![]() |
hooni | 2013.11.20 | 13798 |
243 | Develop | [ios] iOS In App Purchase 코드 부분 샘플 1 | hooni | 2013.11.20 | 11788 |
242 | Develop | [ios] iOS In App Purchase 코드 부분 샘플 2 | hooni | 2013.11.20 | 11322 |