Contents

조회 수 2084 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
937 Develop [ios] 애플 앱스토어 IDFA 리뷰 정책 변경 안내 file hooni 2014.05.07 5007
936 Develop [android] 코드에서 문자열로 Resource 가져오기 hooni 2015.07.09 5015
935 Develop [ios] How to set up clang formatter hooni 2015.09.17 5040
934 Develop [ios] 아이폰 GPS 사용하기 hooni 2014.05.24 5043
933 Develop [ios] Xcode를 사용해서 Static Library 만들기 (시뮬레이터 + 디바이스) file hooni 2015.01.03 5060
932 Develop [c] Mac OS 에 gmp(gmp.h) 라이브러리 설치 hooni 2016.10.03 5067
931 System/OS [mac] 맥OSX에서 NTFS 쓰기 기능 활성화 hooni 2014.03.12 5086
930 Database [mysql] MySql 에서 정렬 후 그룹 하는 방법 hooni 2015.05.07 5111
929 Develop [ios] DatePicker iOS 6.x 이하 디자인. file hooni 2014.04.10 5124
928 Develop [ios] Pod 특정 버전 설치하고 사용하기 hooni 2022.05.28 5168
927 Develop [ios] 기본 네비게이션바의 타이틀, back버튼 위치와 속성 변경 file hooni 2016.05.16 5180
926 Develop [swift] UIView에서 subview 찾기 hooni 2022.12.09 5194
Board Pagination Prev 1 ... 16 17 18 19 20 21 22 23 24 25 ... 99 Next
/ 99