2016-04-27 93 views
0

我有一個問題發送口述文本到另一個接口控制器。發送WatchKit語音識別的文本到另一個接口控制器

這是我的代碼:

- (IBAction)voiceRecognition { 

    [self presentTextInputControllerWithSuggestions:nil allowedInputMode:WKTextInputModePlain completion:^(NSArray *results) { 

     NSLog(@"results: %@", results); 

     NSString *wordKey = [NSString stringWithFormat:@"%@",results]; 
     NSDictionary *dict = @{@"kWord":wordKey}; 
     [self pushControllerWithName:@"Dictionary" context:dict]; 

    }]; 
} 

日誌:

觀看擴展[3185:2835671]的結果:(你好)

獲取數據從其它接口控制器:

- (void)awakeWithContext:(id)context { 
    [super awakeWithContext:context]; 

    NSDictionary *dict = (NSDictionary *)context; 
    [_word setText:dict[@"kWord"]]; 

    NSLog(@"The Word is %@",[dict description]); 

} 

日誌:

觀看擴展[3185:2835671]的話是{ 千字= 「(\ n你好\ n)的」; }

這裏是一個截屏,顯示我的問題:

enter image description here

(應該顯示單詞Hello。我該如何解決這個問題?

回答

1

您使用stringWithFormat數組設置爲一個字符串。

這發生["Hello"]並將其正確轉換爲文字"(\n Hello\n)"

因爲這串具有換行,它不能被顯示在一行上。您的故事板WKInterfaceLabel行數可能設置爲1,因此它只會顯示第一行,即(

你怎麼解決這個問題?

  • 如果你只在第一個字興趣,使用results.firstObject並傳遞一個字作爲您kWord密鑰字符串值。

    NSDictionary *dict = @{@"kWord": results.firstObject}; 
    
  • 否則,傳遞整個陣列的值,並有目的地接口控制器處理結果的陣列需要。

    NSDictionary *dict = @{@"kWord": results}; 
    

你也可能要更改的行數顯示整個聽寫文本,來處理其中的文本不適合在一行的情況。

其他選項:

如果你真的打算送聽寫文本爲單詞的單個字符串,可以使用

NSString *wordKey = [results componentsJoinedByString:@" "]