Develop
2015.01.03 14:53
[ios] Thread Loop 내에서 UI 업데이트 방법
조회 수 853 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
793 | Develop | [jsp][php] 간단한 강좌 자료.. | hooni | 2003.04.23 | 8109 |
792 | Develop | [c] 학생명단 관리 프로그램 소스 ㅋㅋ 1 | hooni | 2003.04.23 | 7771 |
791 | Develop | [c] 근의 공식으로 2차방정식 풀기.. | hooni | 2003.04.23 | 7775 |
790 | Develop | [c] 이진트리(binary tree)의 운행.. | hooni | 2003.04.23 | 8268 |
789 | Develop | [c] 이진트리(binary tree)의 특성 | hooni | 2003.04.23 | 9608 |
788 | Develop | [자료구조] 트리(tree) 용어정리 | hooni | 2003.04.23 | 11067 |
787 | Develop | [c] 농구팀 점수 산출 프로그램 소스 | hooni | 2003.04.23 | 7383 |
786 | Develop | [c] 단어 입력/수정 프로그램 소스 | hooni | 2003.04.23 | 7026 |
785 | Develop | [c] 정사각배열의 서브 배열의 최대 값 구하기 | hooni | 2003.04.23 | 7022 |
784 | Develop | [c] 이진트리(binary tree) 샘플소스 (삽입, 삭제, 운행) | hooni | 2003.04.23 | 7807 |
783 | Develop | [java] 컴포넌트 배치 ㅡ.,ㅡ; | hooni | 2003.04.23 | 7154 |
782 | Develop | [html] 메타태그 사용예.. 은지나 바라~ | hooni | 2003.04.23 | 7234 |