2011-03-15 4 views
0

NSThread를 만들 때 프로세스에 알리고 싶은 번호를 전달합니다. 숫자를 설정하는 방법을 이해할 수 있지만 스레드 선택기 메서드에서 숫자를 읽는 방법을 알아낼 수 없으므로 타이머에 전달할 수 있습니다.개체를 NSThread 선택기로 전달

어떻게 하시겠습니까?

-(void) setthread 
{ 

// 여기에 고급 선택기에 수를 통과

NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:[NSNumber numberWithInt:index]];/ 
    [timerThread setThreadPriority:0.5]; 
    [timerThread start]; //start the thread 

} 

//

-(void) startTimerThread 
{ 


    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; 
    [[NSTimer scheduledTimerWithTimeInterval: 0.1 
             target: self 
            selector: @selector(timerTick:) 
            userInfo: thenumberhere 
            repeats: YES] retain]; 

    [runLoop run]; 
    [pool release]; 
} 

- (void)timerTick:(NSTimer *)timer 
{ 
    //code 
} 

답변

4

당신은 당신의 선택의 잘못을 지정하고 있습니다 : 그것은과 같이, 마지막에 :을 가져야한다

@selector(startTimerThread) // we are missing ':' at the end 

:

@selector(startTimerThread:) 

이 그것을 하나 개의 매개 변수를 선택기입니다 나타냅니다.

그런 다음 startTimerThread 방법의 매개 변수에 걸릴 :

-(void) startTimerThread:(NSNumber *)myNumber { 
    // ... etc 
+1

이 답변은 비슷한 문제에 대한 저를 도왔습니다. +1;) – craig1231

0

작동하지 않습니다이 셀렉터에 전달 된 값을 읽는 방법을하지 않습니다 .. this :

NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread:) object:[NSNumber numberWithInt:index]]; 


-(void) startTimerThread:(NSNumber *)thenumberhere 
{ 


    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; 
    [[NSTimer scheduledTimerWithTimeInterval: 0.1 
             target: self 
            selector: @selector(timerTick:) 
            userInfo: thenumberhere 
            repeats: YES] retain]; 

    [runLoop run]; 
    [pool release]; 
} 

당신은 매개 변수로 선택기와 함께 전달한 객체를 구현 한 메소드에 추가하는 것을 잊어 버렸습니다.

+0

이유 NSRunLoop을 사용 ?? – IKKA

+0

1 년 전, 질문과 함께 제공된 코드를 수정했습니다. 더비 비트에게 물어보십시오. – Jake