Develop
2015.01.03 14:53
[ios] Thread Loop 내에서 UI 업데이트 방법
조회 수 925 댓글 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] 자료구조 그래프(graph) 소스코드 - 확인해볼 것..
![]() |
hooni | 2013.04.23 | 8011 |
548 | Develop | [c++] Win32API를 이용한 ExitWindowsEx 사용한 예제코드 | hooni | 2013.04.23 | 7997 |
547 | Develop |
[doc] Equation Solving에 대한 발표자료..
![]() |
hooni | 2013.04.23 | 7996 |
546 | Develop |
[c] 퀵정렬(quick sort) 예제 소스..
![]() |
hooni | 2013.04.23 | 7992 |
545 | Develop |
[chm] C++ 문법 가이드
![]() |
hooni | 2013.04.23 | 7970 |
544 | Develop |
[c++] 트리컨트롤 스텝 3 예제..
![]() |
hooni | 2013.04.23 | 7965 |
543 | Develop |
[c] 마우스 따라다니는 고양이 - 네코95 (WinAPI)
![]() |
hooni | 2013.04.23 | 7964 |
542 | Develop |
[js] 양원님이 공유해 주신 유료(5$란다ㅋ) 자료 ㅋㅋ
![]() |
hooni | 2013.04.23 | 7948 |
541 | Develop |
[c] UCP/TCP 채팅 소스 - 정리해야 함..
![]() |
hooni | 2003.04.23 | 7947 |
540 | Develop | [js] 자바스크립트 이벤트 핸들.. | hooni | 2003.04.23 | 7946 |
539 | Develop |
[java] 마우스 버튼 테스트.. swing..
![]() |
hooni | 2013.04.23 | 7906 |
538 | Develop | [c] OpenGL 색 입방체의 회전(입체) | hooni | 2003.04.23 | 7897 |