我在执行不同的NSTask时遇到问题。相同launchPath
,不同arguments
。我有一个类的实例管理自己的NSTask
对象,并取决于这些实例初始化与 - 依赖NSTask
对象正在创建。我有两个初始化:NSTask只执行一次
// Method for finished task
- (void)taskFinished:(NSNotification *)aNotification {
[myTask release];
myTask = nil;
[self createTask];
}
// Designated initializer
- (id) init {
self = [super init];
if (self != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(taskFinished:)
name:NSTaskDidTerminateNotification
object:nil];
[self createTask];
}
return self;
}
// Convenience initializer
- (id)initWithCommand:(NSString *)subCommand {
self = [self init];
if (self)
{
[self setCommand:subCommand];
}
return self;
}
而且这里的createTask
方法:
- (void)createTask {
// myTask is a property defined as NSTask*
myTask = [[NSTask alloc] init];
[myTask setLaunchPath:@"/usr/bin/executable"];
}
的行动通过(使用PXSourceList作为包装器)中NSOutlineView选择不同行执行:
- (void)sourceListSelectionDidChange:(NSNotification *)notification {
id sourceList = [notification object];
NSIndexSet *selection = [sourceList selectedRowIndexes];
NSString *identifier = [[sourceList itemAtRow:[selection firstIndex]] identifier];
// this way `/usr/bin/executable ${identifier}` is being created
MyCommand *command = [[MyCommand alloc] initWithSubcommand:identifier];
// this method executes [myTask launch];
[command execute]
}
问题是只有第一个被执行。第二个甚至不会触发“点击”事件(通过目标操作)。我认为这可能是我尝试使用launchPath的原因,因为简单的/bin/ls
工作正常。终端中的相同命令具有0的返回值(即一切正常)。任何指南或陷阱都非常感谢。
我认为我们需要看到操作方法。你在启动任务时是否禁用了按钮?这似乎是必要的,因为你只有一个'myTask'实例变量。 – 2010-05-17 06:42:12
我更新了请求的细节代码。 – Eimantas 2010-05-17 13:22:04