title: Intercept custom action on TabView
body: <p>I was recommended to use the standard components so, instead of building my own TabView, I used SwiftUI's TabView. I only need a custom action that doesn't activate a view but triggers an action.</p>
  <p>I almost succeeded to do it with the following code:</p>
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      @State var value: Int = 1
  
      private var adapterValue: Binding&lt;Int&gt; {
          Binding&lt;Int&gt;(get: {
              if (self.value == 4) {
                  return 2
              } else {
                  return self.value
              }
          }, set: {
              if $0 == 4 {
                  self.value = 2
              } else {
                  self.value = $0
              }
          })
      }
  
      var body: some View {
          TabView(selection: self.adapterValue, content:  {
              Text(&quot;A&quot;).tabItem {
                  VStack {
                      Text(&quot;A&quot;)
                  }
              }
              .tag(1)
              
              Text(&quot;B&quot;).tabItem {
                  VStack {
                      Text(&quot;B&quot;)
                  }
              }
              .tag(2)
              
              Text(&quot;C&quot;).tabItem {
                  VStack {
                      Text(&quot;C&quot;)
                  }
              }
              .tag(3)
              
              Text(&quot;D&quot;).tabItem {
                  VStack {
                      Text(&quot;D&quot;)
                  }
              }
              .tag(4)
          })
      }
  }
  </code></pre>
  <p>It works well, except for when tab 2 (B) is activated and I press 4 (D), tab 4 (D) is activated. This should not happen, but I'm unsure what I'm missing.</p>
  <p>I would like to understand the reason why this doesn't work as I expected and how I can prevent this behavior.</p>