1

I am very new to objective c and OpenEars so please forgive me if I have some messy code and if I am lost in very simple problem.

Anyhow, I have two controllers in this application. The first being the default ViewController and the second one being a new one that I made called ReplyManagerController.

The code in the ViewController basically uses the one in the tutorial with some (maybe more some) changes.

EDIT:

The app is supposed to be a basic app where a user says something and the app replies.

But the original problem was that I could not get the string to display or TTS to work when my ViewController got it's string from another class/controller.

The answer in my below mentions that it was probably because my other class was calling my ViewController without the self.fliteController initialized.

How would I initialize the ViewController with self.fliteController initialized?

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;

}

Any help will be great! Thanks!

ReplyManager.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];
      }
}

EDIT 2:

viewDidLoad Method

 - (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];
}

2 Answers2

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

Here you init a new ViewController which does not have self.fliteController defined most likely. You need to reuse previos controller, for example like this:

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

So you can use already initialized viewController later. Overall it is better to initialize objects like viewController or replyController beforehand, not inside callback methods.

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
  • Sorry for the late comment, but I get an error saying: 'No visible @interface for ReplyManager declares the selector selectReply:viewController:'. Why is that? – user2438817 Jun 19 '16 at 23:55
  • It should be related to some other code you wrote. You probably need to ask another question and provide all necessary details. – Nikolay Shmyrev Jun 20 '16 at 08:33
  • Hi again, I fixed the previous error and have did it as you have shown, but it is still not working. There is still no voice speaking when I use the reply manager. Did I need to make ReplyManager a subclass or something like that? – user2438817 Jun 29 '16 at 01:30
0

It sounds like a timing issue where you're trying to use fliteController before it's been initialized.

In your ViewController class, where do you assign a value to the fliteController property? In an initializer? -(void)viewDidLoad?

In ReplyManagerController add:

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

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

Does the above fix the problem? If so, you're probably initializing fliteController in -(void)viewDidLoad. What's the result of the two NSLog statements?

Lee Fastenau
  • 444
  • 3
  • 14
  • Yes I load the fliteController in - (void)viewDidLoad. – user2438817 Jul 06 '16 at 07:01
  • And I do get a null on the first NSLog and get something in the next – user2438817 Jul 06 '16 at 07:12
  • Well, there you go. The problem was that you alloc/init'd `ViewController`, but `-(void)viewDidLoad` never got called to initialize `fliteController`. Adding `[getReply.view layoutIfNeeded];` (or simply referencing `view`) caused `viewDidLoad` to run. – Lee Fastenau Jul 06 '16 at 07:17
  • Alternatively, you could initialize `fliteController` in `ViewController`'s designated initializer. e.g., `-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil` Any initialization that's not dependent on the view controller's view should probably occur there anyway. – Lee Fastenau Jul 06 '16 at 07:20
  • Oddly though the TTS still doesn't seem to work. Neither does the string show up on view. I feel like I messed somewhere big but I can't seem to find where – user2438817 Jul 06 '16 at 07:41
  • The `ViewController` instance created in `ReplyManager` is likely deallocating as soon as the method exits. After `ViewController* getReply = [[ViewController alloc] init];` add: `[[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:getReply animated:YES completion:nil];` – Lee Fastenau Jul 06 '16 at 08:16
  • 1
    I think that did it!~ Thank you! – user2438817 Jul 06 '16 at 08:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116631/discussion-between-user2438817-and-lee-fastenau). – user2438817 Jul 07 '16 at 01:23