cocoapodsのneetmstranslatorを使って翻訳アプリを作ろうと思っているのですが、UILabelに翻訳結果を表示させることができません。
http://qiita.com/mironal/items/875234f044dcb192a50e
※イメージとしては、textfield(translator.text)内の単語を英語(en)に翻訳し、label(result.text)に結果を表示させたいと考えてます。
あれこれ原因を探った結果、恐らくneetmstranslatorの処理に時間がかかり、先にlabelがデフォルトで表示されているのが原因ということまでは分かりました。
その代わりに一時的にstring変数に保存し、別のクラスでnsnotificationを使用して呼び戻そう考えたのですが、いまいち「どこに、どの様に」nsnotificationを書き込めば良いか分かりません。
ど素人でも分かるように説明していただけると幸いです。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
《自力でやってみた結果、以下のコードのようになりました》
※result.text = MyNotificationの所で「Cannot assign a value of type "NSNotification" to a value of type " String!"」というエラーが出てしまいます・・・・・
import UIKit
let MyNotification = NSNotification(name: "MyNotification", object: nil)
class ViewController: UIViewController {
@IBOutlet var translation: UITextField! @IBOutlet var result: UITextView! var t = NMSTranslator() override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: "myFunc:",name: "MyNotification" , object: nil) } @IBAction func translation(sender: AnyObject) { NSNotificationCenter.defaultCenter().postNotificationName("MyNotification", object: nil) var t = NMSTranslator() t.initializeTranslatorWithClientID ("+++", clientSecret: "+++") t.transrateWithText(translation.text, to: "en", success: { (response, String) -> Void in "MyNotification"}) {(response, data, err) -> Void in "MyNotification"} } func myFunc(notification: NSNotification) { result.text = MyNotification NSNotificationCenter.defaultCenter().removeObserver(self , name: "MyNotification", object: nil) }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/07/19 15:13
2015/07/19 15:24
2015/07/19 16:06