Contents

조회 수 925 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
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

?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
481 Develop Mac OS 에 Jenkins 설치하기 (Homebrew) 2 file hooni 2017.03.15 8397
480 Develop [java] 채팅 프로그램.. swing 사용.. file hooni 2013.04.23 8371
479 Develop [c] 콘솔에서 패스워드 입력시 문자 보이지 않게 하는 코드 file hooni 2013.04.23 8359
478 Develop [c] 암호 알고리즘 소스.. file hooni 2013.04.23 8353
477 Develop [js] 쿠키(cookie)에 대한 설명과 예제.. hooni 2003.04.23 8351
476 Develop [js] selectbox 선택 후 input 박스에 적용 hooni 2013.04.23 8348
475 Develop [swift] NotificationCenter 간단 예제 file hooni 2021.01.27 8340
474 Develop [c] 이진트리(binary tree)의 운행.. hooni 2003.04.23 8309
473 Develop [c++] 레지스트리 편집하는 부분..ㅡㅡ; file hooni 2013.04.23 8304
472 Develop [c++] 현승이가 준 메신저 소스.. ㅋㅋ file hooni 2013.04.23 8299
471 Develop [c] 문자열 정렬 함수 qsort() hooni 2003.04.23 8274
470 Develop [php] 마시마로 캐릭터 방명록 file hooni 2003.04.23 8272
Board Pagination Prev 1 ... 26 27 28 29 30 31 32 33 34 35 ... 71 Next
/ 71