Develop
2015.01.03 14:53
[ios] Thread Loop 내에서 UI 업데이트 방법
조회 수 924 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
817 | Develop | [Xcode] 디버깅 옵션 | hooni | 2013.04.23 | 57261 |
816 | Develop | [vb] 비주얼 베이직 205페이지 | hooni | 2003.04.23 | 56137 |
815 | Develop | [vb] 비쥬얼 베이직 기출 문제 연습 | hooni | 2003.04.23 | 55241 |
814 | Develop | [vb] 비주얼 베이직 173페이지 | hooni | 2003.04.23 | 54915 |
813 | Develop | [vb] 비쥬얼 베이직 기출 문제 연습2 | hooni | 2003.04.23 | 54447 |
812 | Develop | [iphone] view에 대한 조사 ㅡ,.ㅡ; | hooni | 2013.04.23 | 54117 |
811 | Develop | [vb] 비쥬얼 베이직으로.. | hooni | 2003.04.23 | 53132 |
810 | Develop | [android] GCM 사용하기 1 (GCM 서비스 신청하기) | hooni | 2013.07.06 | 51424 |
809 | Develop | [android]개발 가이드 및 한글화 문서 | hooni | 2013.04.23 | 47562 |
808 | Develop | [android] ArrayAdapter를 이용하여 출력하기 | hooni | 2013.04.23 | 47392 |
807 | Develop | [ios] UIAlertView 초간단 샘플 ㅎㅎ | hooni | 2013.10.14 | 46196 |
806 | Develop | [ios] 배열(NSArray) 연산과 간단한 애니메이션(split images) | hooni | 2013.10.31 | 45558 |