【问题标题】:Display output from nstask error显示来自 nstask 错误的输出
【发布时间】:2013-01-12 19:54:22
【问题描述】:

好的,所以我正在尝试制作一个基本上是脚本 GUI 的应用程序,我无法正常工作的部分是显示脚本的输出,起初我有它,所以它应该是我完成任务后的所有内容已经完成了这不用说是没有好处的。我现在所处的位置是我可以在 uitextview 中实时获取输出,但是随着新信息的传递,它会经过几圈传递,使其不可读,我在示例中使用了脚本 apt-get update。我是一个新的越狱开发者,是的,我的应用程序以 root 权限运行我唯一的问题是输出......我的代码是这样的:

#import "RootViewController.h"

@implementation RootViewController
@synthesize navItem;
- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:       [[UIScreen mainScreen] applicationFrame]]        autorelease];
    self.view.backgroundColor = [UIColor        redColor];

    navBar = [[UINavigationBar alloc] init];
    navBar.frame = CGRectMake(0, 0,         self.view.frame.size.width, 44);

    navItem = [[[UINavigationItem alloc]
    initWithTitle:@"GUI"] autorelease];
    navBar.barStyle = UIBarStyleDefault;
    navBar.items = [NSArray     arrayWithObject:navItem];
    [self.view addSubview:navBar];

    NSPipe *pipe = [NSPipe pipe];

    _fileHandle = [pipe fileHandleForReading];
    [_fileHandle readInBackgroundAndNotify];

    task = [[NSTask alloc] init];
    [task setLaunchPath:@"/usr/bin/apt-get"];
    [task setStandardOutput: pipe];
    [task setStandardError: pipe];

        NSArray *arguments;
    arguments = [NSArray arrayWithObjects:     @"update", nil];
    [task setArguments: arguments];

    [task launch];

}
-(id)init
{
    [super init];
    [[NSNotificationCenter defaultCenter]     addObserver:self
    selector:@selector( readPipe: )
      name:NSFileHandleReadCompletionNotification 
    object:nil];
    return self;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]     removeObserver:self];
}

-(void)readPipe: (NSNotification *)notification
{
    NSData *data;
    NSString *text;

    if( [notification object] != _fileHandle )
        return;

    data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
    text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];


    navItem.title = text;

    titleTextField = [[UITextView alloc]   initWithFrame: CGRectMake(0, 40, 320, 350)];
    [titleTextField setBackgroundColor:[UIColor     clearColor]];
    titleTextField.text = text;
    titleTextField.editable = YES;
    titleTextField.scrollEnabled = YES;
    titleTextField.autoresizingMask =UIViewAutoresizingFlexibleHeight;
    [self.view addSubview: titleTextField];

    [text release];
    if( task )
    [_fileHandle readInBackgroundAndNotify];

}

@end

【问题讨论】:

  • 在以下行放一个断点:“data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];” .调试器甚至会走到那里吗?

标签: iphone objective-c xcode nstask


【解决方案1】:

每次使用任务的新输出调用 readPipe 时,您都会使用 相同的框架矩形创建一个新的 UITextView。这就是新文本与之前显示的文本重叠的原因。

您应该使用单个UITextView 并始终追加 readPipe 中的新文本,或者为每个文本视图创建不同的框架。

【讨论】:

  • 你应该只在主线程上使用 UITextView。
  • @bbum:readInBackgroundAndNotify 的文档声明:“......并在当前线程上发布 NSFileHandleReadCompletionNotification 通知......” - 如果“当前线程”表示 readInBackgroundAndNotify 所在的线程已启动,则通知应再次出现在主线程上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 2019-06-03
  • 2016-07-24
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多