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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
169 | Develop | [coding] 공부해야 하는거 ㅋㅋ | hooni | 2017.06.27 | 0 |
168 | Develop | [coding] Find all anagrams in a string | hooni | 2017.06.27 | 1146 |
167 | Develop | [chm] 윈도우즈에서 디버깅 기법(Debugging Applications) | hooni | 2013.04.23 | 7527 |
166 | Develop | [chm] 비주얼 C++ 팁 모음 문서 | hooni | 2013.04.23 | 7500 |
165 | Develop | [chm] Programming Applications for Microsoft Windows | hooni | 2013.04.23 | 7273 |
164 | Develop | [chm] C++ 문법 가이드 | hooni | 2013.04.23 | 7942 |
163 | Develop | [c/c++] Makefile 사용하기.. ㅋㅋ | hooni | 2003.04.23 | 7321 |
162 | Develop | [c++]현승이가 보내준 암호화 모듈 AES라인델.. | hooni | 2003.04.23 | 10715 |
161 | Develop | [c++]현승이가 보내준 동영상 암호화 자료.. 볼것.. | hooni | 2003.04.23 | 9729 |
160 | Develop | [c++][mfc] 파일 입출력 샘플 (한줄씩 읽어서 다른 파일에 쓰기) | hooni | 2013.04.23 | 14995 |
159 | Develop | [c++] 현승이가 준 메신저 소스.. ㅋㅋ | hooni | 2013.04.23 | 8275 |
158 | Develop | [c++] 현승이가 준 P2P 프로그램 소스 ㅋㅋ | hooni | 2013.04.23 | 12072 |