1

我对 Objective c 和 OpenEars 很陌生,所以如果我有一些乱七八糟的代码并且如果我迷失在非常简单的问题中,请原谅我。

无论如何,我在这个应用程序中有两个控制器。第一个是默认 ViewController,第二个是我创建的新 ViewController,称为ReplyManagerController。

ViewController 中的代码基本上使用了教程中的代码,并进行了一些(可能更多)更改。

编辑:

该应用程序应该是一个基本的应用程序,用户说些什么,应用程序会回复。

但最初的问题是,当我的 ViewController 从另一个类/控制器获取字符串时,我无法让字符串显示或 TTS 工作。

我下面的答案提到这可能是因为我的其他类在没有初始化 self.fliteController 的情况下调用了我的 ViewController。

我将如何使用 self.fliteController 初始化 ViewController?

视图控制器.m

- (void) pocketsphinxDidReceiveHypothesis:(NSString *)hypothesis recognitionScore:(NSString *)recognitionScore utteranceID:(NSString *)utteranceID {

NSString *strResult = hypothesis; //speech to text to string

ReplyManager* replyObject = [[ReplyManager alloc] init];
[replyObject speechResult:(NSString*)strResult viewController:self];
}

- (void) getReply:(NSString*)replyStr{

[self.fliteController say:replyStr withVoice:self.slt];

[self updateText:replyStr];
}

- (IBAction)updateText:(NSString*)replyStr{
labelOne.text = replyStr;
labelOne.adjustsFontSizeToFitWidth = YES;
labelOne.minimumFontSize = 0;

}

任何帮助都会很棒!谢谢!

回复管理器.m

  - (void) speechResult:(NSString*)strResult {

    NSString *replystr;
    NSString *greetings = @"Hi!";
    NSString *sorry = @"Sorry I didn't catch that?";

    ViewController* getReply = [[ViewController alloc] init];

    if ([strResult isEqualToString:@"HELLO"])
      { 
       replystr = greetings;
       [getReply getReply:(NSString*)replystr];
      }
    else
      {
       replystr = sorry;
       [getReply getReply:(NSString*)replystr];
      }
}

编辑2:

viewDidLoad 方法

 - (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.

   self.fliteController = [[OEFliteController alloc] init];
   self.slt = [[Slt alloc] init];

   self.openEarsEventsObserver = [[OEEventsObserver alloc] init];
   [self.openEarsEventsObserver setDelegate:self];
}
4

2 回答 2

1
ViewController* getReply = [[ViewController alloc] init];

在这里你很可能初始化一个ViewController没有self.fliteController定义的新的。您需要重用 previos 控制器,例如:

   [replyObject speechResult:(NSString*)strResult viewController:self];

所以你可以在以后使用已经初始化viewController的。总的来说,最好提前初始化对象viewControllerreplyController而不是在回调方法中。

于 2016-05-31T16:05:43.877 回答
0

这听起来像是一个时间问题,您在fliteController初始化之前尝试使用它。

在您的ViewController班级中,您在哪里为属性赋值fliteController?在初始化程序中?-(void)viewDidLoad?

补充ReplyManagerController

ViewController* getReply = [[ViewController alloc] init];

// Add these lines
NSLog(getReply.fliteController); // Is this nil?
[getReply.view layoutIfNeeded];
NSLog(getReply.fliteController); // Is it still nil?

以上是否解决了问题?如果是这样,您可能正在初始化fliteController. -(void)viewDidLoad这两种NSLog说法的结果是什么?

于 2016-07-06T06:30:01.540 回答