Develop
2015.01.03 14:53
[ios] Thread Loop 내에서 UI 업데이트 방법
조회 수 2083 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
553 | Develop | [c] OpenGL 직사각형(2D) 크기 확대/축소 | hooni | 2003.04.23 | 10089 |
552 | Develop |
[c] 공용체를 이용해 MSB를 LSB로 변환
![]() |
hooni | 2013.04.23 | 10095 |
551 | Develop | [c] 파일(File)의 연결과 연결 해제 (link, unlink, chown) | hooni | 2013.04.23 | 10106 |
550 | Develop | [js] 폼(form) 전송시 중복 클릭 방지 간단한 구문 | hooni | 2013.04.23 | 10115 |
549 | Develop | [php] 초간단 게시판 페이지 분할 알고리즘 | hooni | 2003.04.23 | 10119 |
548 | Develop |
[c] 디피-헬만 키교환(Diffie–Hellman key exchange) 샘플 코드.. ㅋㄷ
![]() |
hooni | 2013.04.23 | 10129 |
547 | Develop |
[c] 도메인 소켓(Unix Domain Socket) UDP
![]() |
hooni | 2013.04.23 | 10175 |
546 | Develop |
[c++] MFC로 만든 디렉토리/파일 파인더
![]() |
hooni | 2013.04.23 | 10179 |
545 | Develop | [js] jQjuery $ 활용 | hooni | 2013.12.17 | 10198 |
544 | Develop |
[ios] UUID 생성 + Key Chain 연동
![]() |
hooni | 2016.05.13 | 10202 |
543 | Develop |
[java] RGB 색상 조절 버튼.. 스윙(swing)
![]() |
hooni | 2013.04.23 | 10211 |
542 | Etc |
논문(BHO)에 들어갈 내용 집에가서 테스트 해볼 것..
![]() |
hooni | 2013.05.28 | 10230 |