Q&A
前提
swift初学者で、初めてAppleWatchのアプリ開発を行なっています。
タイトルのとおり、画面間のセグエによる値渡しの方法についてうまくできなくて困っています。
iOSでのセグエによる値渡しは実現できたのですが、WatchOSはiOSとは考え方が違っていて実現できていません。
WatchOSに関する記事も少なく、自分で解決できなかったため質問させていただきました。
実現したいこと
1ページ目のボタンをタップしたら、ボタンのコードに記載されているテキストデータを2ページ目のラベルに表示させるようにしたいと思っています。
コードは下記のとおりです
//InterfaceController import WatchKit import Foundation class InterfaceController: WKInterfaceController { //ボタンを押して値渡し @IBAction func buttonAction() { //ダイアログ表示 let okButtonAction = WKAlertAction(title:"OK" , style: .default ){() -> Void in //値渡しコード func contextForSegue(withIdentifier segueIdentifier: String) -> Any? { if segueIdentifier == "GoNext"{ return "成功" } return nil } } let cancelButtonAction = WKAlertAction(title:"キャンセル" , style: .default ){() -> Void in } presentAlert(withTitle: "次ページへデータを送ります", message: "よろしいですか?", preferredStyle: .alert, actions: [okButtonAction, cancelButtonAction]) } override func awake(withContext context: Any?) { // Configure interface objects here. } override func willActivate() { // This method is called when watch view controller is about to be visible to user } override func didDeactivate() { // This method is called when watch view controller is no longer visible } }
//nextInterfaceController import Foundation import WatchKit class nextInterfaceController: WKInterfaceController { @IBOutlet weak var textLabel: WKInterfaceLabel! //値の格納変数 var context = "" override func awake(withContext context: Any?) { // Configure interface objects here. //データをラベルへ表示 self.textLabel.setText(context as! String) } override func willActivate() { // This method is called when watch view controller is about to be visible to user } override func didDeactivate() { // This method is called when watch view controller is no longer visible } }
コードは以上です。
この状態でボタンを1ページ目のボタンをタップしても2枚目に値が渡されないです。
コードをどう修正すればいいのかがわかりません。
1ページ目
2ページ目(ボタンタップ後)
このように何も表示されません
試したこと
試しにボタンアクション外に値渡しコードを記載した結果、2ページ目に値が渡されているのですが、常にデータが渡されているため、任意のタイミングで値を渡すことができない状態です。
コードは下記のとおりです。
import WatchKit import Foundation class InterfaceController: WKInterfaceController { @IBAction func buttonAction() { //ダイアログ表示 let okButtonAction = WKAlertAction(title:"OK" , style: .default ){() -> Void in } let cancelButtonAction = WKAlertAction(title:"キャンセル" , style: .default ){() -> Void in } presentAlert(withTitle: "次ページへデータを送ります", message: "よろしいですか?", preferredStyle: .alert, actions: [okButtonAction, cancelButtonAction]) } //値渡しコード override func contextForSegue(withIdentifier segueIdentifier: String) -> Any? { if segueIdentifier == "GoNext"{ return "成功" } return nil } override func awake(withContext context: Any?) { // Configure interface objects here. } override func willActivate() { // This method is called when watch view controller is about to be visible to user } override func didDeactivate() { // This method is called when watch view controller is no longer visible } }
2ページ目(ボタンタップ前)
このようにデータは受け渡されているのが確認できます。
ボタンをタップした時になぜデータが送られないのかがわかりません。
もしかしたら、2ページ目のデータの受け取り方にミスがあるのかもしれませんが、正しいデータの受け取り方がわかりません。
よければご教授いただきたいです。
またそれ以外で間違いがあればご指摘いただきたいです。
よろしくお願致します。
補足情報
参考URL
Class WKInterfaceController
https://developer.apple.com/documentation/watchkit/wkinterfacecontroller
func awake(withContext context: Any?)
https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/1619543-awake
func contextForSegue(withIdentifier segueIdentifier: String) -> Any?
https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/1619522-contextforsegue
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/07/22 01:59