Develop
2015.01.03 14:53
[ios] Thread Loop 내에서 UI 업데이트 방법
조회 수 839 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
745 | Develop | [js] 자바스크립트(Javascript) 코드를 동적으로 삽입하는 방법.. | hooni | 2013.04.23 | 22873 |
744 | Develop | [ios] UILabel top alignㅎㅎ | hooni | 2013.04.23 | 22838 |
743 | Develop | 밸런싱 로봇.. 최종.. (관련 논문도 첨부) ㅋㅋ | hooni | 2013.04.23 | 22816 |
742 | Develop | [vb] 문자열에서 태그 제거함수 (Visual Basic) | hooni | 2013.04.23 | 22617 |
741 | Develop | [iphone] 화면 전환 Portrait & Landscape Mode | hooni | 2013.04.23 | 22306 |
740 | Develop | [doc] Json Framework 설치와 사용 | hooni | 2013.04.23 | 21914 |
739 | Develop | [c] 재미있는 코딩.. | hooni | 2003.04.23 | 21314 |
738 | Develop | 프로그래밍에서 foo, bar 함수의 유래 | hooni | 2013.06.25 | 21238 |
737 | Develop | [c] 포인터 학습용 예제 소스 코드 | hooni | 2013.04.23 | 21025 |
736 | Develop | [c] 이진트리/트리 순회법 코드(전위/중위/후위) 5 | hooni | 2015.07.02 | 20914 |
735 | Develop | git 브런치 배우기 (링크) | hooni | 2013.07.09 | 20571 |
734 | Develop | [c++] 디렉토리 내의 파일 찾기 FindFirstFile() 함수 6 | hooni | 2013.04.23 | 20564 |