LoginSignup
2

More than 1 year has passed since last update.

【謎すぎ】SwiftUI encountered an issue when pushing aNavigationLink エラー

Posted at

謎すぎる「SwiftUI encountered an issue when pushing aNavigationLink」

以下のバグにだいぶ苦しみました。

一応画面遷移はちゃんとできるし、一見問題なさそうだけどエラーログ出ているのが気持ち悪かったので対応した。どうせアプリリースの時にappleさんから指摘されそうだしね。

SwiftUI encountered an issue when pushing aNavigationLink. Please file a bug.

解決策ググっても全然出てこなかったので、かなり解消に時間がかかりました。。
備忘として雑に残します。

ちなみに本バグは数々のロジックのバグを総称して上記のようなアラートを出しています。
あくまで参考程度に見ていただければ幸いです。

解決策

結論:NavigationLinkをForEachまたListから出してください。

Before:ForEachの中にNavigationLinkを配置していた。

ForEach(self.Memoes.data){i in
   VStack(alignment: .leading, spacing:5){
      NavigationLink(destination: ReadMemoView(data: self.data),isActive:self.$viewControllerModel.ReadMemoViewPushed) {
      Button(action: {
          self.data = i
          self.viewControllerModel.ReadMemoViewPushed.toggle()
      }) {
           Text("テキスト")
          }
      }
   }
}

After:ForEachの外にNavigationLinkを配置する

NavigationLink(destination: ReadMemoView(data: self.data), isActive: self.$viewControllerModel.ReadMemoViewPushed) {
       EmptyView()
}

ForEach(self.Memoes.data){i in
   VStack(alignment: .leading, spacing:5){
      Button(action: {
          self.data = i
          self.viewControllerModel.ReadMemoViewPushed.toggle()
      }) {
           Text("テキスト")
          }
   }
}

ForEachの中でNavigationLinkを行うと繰り返し処理された「isActive」のどれをアクティブ処理するべきか分からなくなってしまうみたい。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2