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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
549 | Develop | [c++] 중복실행 방지(Mutex;뮤텍스 ) 샘플 소스.. ㅋㅋ | hooni | 2013.04.23 | 8741 |
548 | Develop | [c] OpenGL 시어핀스키 가스킷(p.73 - 첫시간) | hooni | 2003.04.23 | 8756 |
547 | Develop | [java] 채팅창 처럼.. swing.. | hooni | 2013.04.23 | 8771 |
546 | Develop | [c] 압축 알고리즘 소스 및 정리 | hooni | 2013.04.23 | 8780 |
545 | Database | [mysql] 쿼리 실행시 ERROR 1366 (HY000) : incorrect string value : for column 3 | hooni | 2017.03.30 | 8787 |
544 | Develop | [c++] 레지스트리 등록 예제 1 | hooni | 2013.04.23 | 8791 |
543 | Develop | [c] 함수 요약 (검색해서 쓰세요..) | hooni | 2003.04.23 | 8795 |
542 | Develop | [c++] mfc 이용한 트레이아이콘(TrayIcon) 클래스 예제 프로젝트 | hooni | 2013.04.23 | 8796 |
541 | Develop | [c] 지나가는 패킷 잡기 | hooni | 2003.04.23 | 8802 |
540 | Develop | [html] SVG(Scalable Vector Graphics) 간단 정리 | hooni | 2014.02.13 | 8834 |
539 | Develop | [c++] 윈도우 프로세스뷰어 분석.. ㅋㅋ | hooni | 2013.04.23 | 8840 |
538 | Develop | [js] 초간단 암호화/복호화 소스.. | hooni | 2003.04.23 | 8843 |