获取传递给NSThread选择器的对象 [英] getting the object passed to the NSThread selector

查看:145
本文介绍了获取传递给NSThread选择器的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个NSThread我传递一个数字,我想让过程知道。我可以理解如何设置数字,但我不知道如何从线程选择器方法中读取数字,以便我可以将其传递给计时器。

When I'm creating an NSThread I pass it a number that I want the process to be aware of. I can understand how to set the number but I cannot figure out how to read the number from the thread selector method so that I can then pass it to a timer.

如何

-(void) setthread
{ 

//将数字传递到选择器中

//passing the number to the selector here fine

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

}

要读取传递给此选择器的值

// don't get how to read the value passed to this selector

-(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
}


推荐答案

您指定的选择器错误:

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

结尾应该有,如下:

@selector(startTimerThread:) 

这表示它是一个选择器,需要一个参数。

This indicates it's a selector which takes one parameter.

然后输入 startTimerThread 方法中的参数:

Then take in the parameter in your startTimerThread method:

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

这篇关于获取传递给NSThread选择器的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆