【发布时间】:2014-09-29 07:29:58
【问题描述】:
我无法在任何 stackoverflow 主题中找到答案。我遇到过线程和类,我想在全局 *.m 文件中使用类控制器变量和操作。没有附加的第二类。 这是代码:
Controller.h
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet NSWindow *Main;
IBOutlet NSButton *myButton;
}
- (void)awakeFromNib;
- (IBAction)action:(id)sender;
- (IBAction)Display(id)sender;
@end
Controller.m
#import "Controller.h"
@implementation Controller
- (void)awakeFromNib
{
//At this point i can reach any variable from Controller.h
}
- (IBAction)Display:(id)sender
{
//Same, i can reach any variable from Controller.h
}
//关闭“}”并从新行开始后,我不能从Controller.h调用[Display]或其他任何东西,我的线程代码在这里,我想编写线程来调用Display,或者使用awakeFromNib 变量、字符串、动作。
线程代码
#include assert.h
#include pthread.h
void* PosixThreadMainRoutine(void* data)
{
// I want to call here, example [Display click:self];
// but i only see [Controller]...
int ac = 0;
while (ac < 8)
{
sleep(1);
printf("Test");
ac++;
}
return NULL;
}
void LaunchThread()
{
// Create the thread using POSIX routines.
pthread_attr_t attr;
pthread_t posixThreadID;
int returnVal;
returnVal = pthread_attr_init(&attr);
assert(!returnVal);
returnVal = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
assert(!returnVal);
int threadError = pthread_create(&posixThreadID, &attr, &PosixThreadMainRoutine, NULL);
returnVal = pthread_attr_destroy(&attr);
assert(!returnVal);
if (threadError != 0)
{
// Report an error.
}
}
@end
线程代码 100% 工作,但我无法在 PosixThreadMainRoutine 中调用任何变量或函数,有人可以解释一下如何做吗? `
【问题讨论】:
标签: multithreading cocoa class xcode5 void