【问题标题】:Objective-C: Multiple Views and Data Coming From An NSTaskObjective-C:来自 NSTask 的多个视图和数据
【发布时间】:2012-06-02 19:21:37
【问题描述】:

所以,我设法让 NSTask 从程序中异步读取,但我是在故事板中的 UIView 类中完成的。 (不是 Obj-C 专家)

我的想法是:我从程序中读取文本,将其放在 UITextView 上,然后通过NSNotificationCenter重复该过程

到目前为止,这是我的代码:

LView.m:

- (void)viewDidLoad
{

    [super viewDidLoad];

    NSPipe *out_pipe = [NSPipe pipe];
    sshoutput = [out_pipe fileHandleForReading];
    [sshoutput readInBackgroundAndNotify];

    utilT = [[NSTask alloc] init];
    [utilT setLaunchPath:@"/usr/bin/utilfc9"];
    [utilT setArguments:[NSArray arrayWithObjects: @"-p", @"-f", @"log.txt", nil]];

    [utilT setStandardOutput: out_pipe];
    [utilT setStandardError: out_pipe];
    [utilT launch];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readPipe:) name:NSFileHandleReadCompletionNotification object:nil];
}

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

    if( [notification object] != sshoutput ) { return };

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

    self.log.text = [self.wifilog.text stringByAppendingFormat: @"\n%@", new_input];

    if( utilT ) {
        [sshoutput readInBackgroundAndNotify];
    }
}

LView.h:

#import <UIKit/UIKit.h>
#import "NSTask.h"

NSTask *sshT;
NSFileHandle *sshoutput;

到目前为止效果很好,我可以毫无问题地实时获取数据。

但是,我怎样才能将这个NSTask 放置在像 AppDelegate 的 application didFinishLaunchingWithOptions 这样更“全局”的地方,然后处理数据并更新另一个类中的多个视图?我试过了,当然我可以在 AppDelegate 中添加log.text = new_input 之类的东西,因为它来自另一个类,包含它并不能解决问题。

您可能已经注意到,我对将其发送到 AppStore 不感兴趣。这是一个供我自己在越狱的 iPhone 上使用的应用程序。 谢谢。

【问题讨论】:

    标签: objective-c ios nsnotificationcenter nstask


    【解决方案1】:

    快速的方法是

    在您希望收到相同通知的所有视图中添加以下内容

    接收者视图

    -(void) viewDidLoad
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(read:)     name:@"ReadTest" object:nil];
    }
    
    //read function
    -(void) read:(NSNotification*)notification
    { // Do something with the notification }
    

    现在在 LView.m

    -(void)readPipe: (NSNotification *)notification
    {
        NSData *data;
        NSString *new_input;
    
        if( [notification object] != sshoutput ) { return };
    
        data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
        new_input = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
        self.log.text = [self.wifilog.text stringByAppendingFormat: @"\n%@", new_input];
    
        if( utilT ) {
            [sshoutput readInBackgroundAndNotify];
        }
        //Add the following
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ReadTest" object:notification]
    }
    

    【讨论】:

    • 它正在工作我收到通知,但我注意到要让它工作,我需要打开 ReceiverView 以便 ViewDidLoad 发生。我怎样才能以编程方式使这种情况发生?谢谢。
    • 移动 [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(read:) name:@"ReadTest" object:nil];到init{函数,也请不要忘记接受答案:)
    • 我设法解决了这个问题,通过放置-(void)awakeFromNib { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(read:) name:@"NewData" object:nil]; } 谢谢你所做的一切!
    【解决方案2】:

    小心,new_input 已分配但未释放 => 内存泄漏

    【讨论】:

    • 我正在使用自动引用计数,所以...无需释放new_input。还是谢谢你。
    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多