Develop
2015.01.03 14:53
[ios] Thread Loop 내에서 UI 업데이트 방법
조회 수 912 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
81 | System/OS | [linux] 아파치설치/설정(모니터링) | hooni | 2003.04.23 | 14205 |
80 | System/OS | [linux] 아파치 설치/설정(proxy) | hooni | 2003.04.23 | 12999 |
79 | System/OS | [linux] 아파치설치/설정 - SSI(Server Side Include) | hooni | 2003.04.23 | 13245 |
78 | System/OS | [linux] 아파치설치/설정(CGI부분) | hooni | 2003.04.23 | 14607 |
77 | System/OS | [linux] 아파치설치/설정 | hooni | 2003.04.23 | 14536 |
76 | System/OS | [linux] 새 하드디스크 추가하기..(내공쌓기) | hooni | 2003.04.23 | 13848 |
75 | System/OS | [linux] 기본적인 설정하기(내공쌓기) | hooni | 2003.04.23 | 14033 |
74 | System/OS | [linux] 계정관리하기(내공쌓기) | hooni | 2003.04.23 | 13225 |
73 | System/OS | [linux] 프로그램 설치방법 (내공쌓기) | hooni | 2003.04.23 | 13467 |
72 | System/OS | [linux] 간단한 vi편집기 사용 명령 | hooni | 2003.04.23 | 13509 |
71 | System/OS | [linux] 기본 명령어 (내공쌓기) | hooni | 2003.04.23 | 14371 |
70 | System/OS | [linux] 리눅스 활용 팁^^ | hooni | 2003.04.23 | 14679 |