I don’t manage to update swift ui with kotlin stat...
# compose-ios
l
I don’t manage to update swift ui with kotlin states, looks like it does not function the way I expect . I did a simple example to showcase the problem: IOS:
Copy code
func makeUIViewController(context: Context) -> UIViewController {
    return Main_iosKt.TestController(
      createUIViewController: { (text, onClick) -> UIViewController in
        return UIHostingController(
          rootView: Button(action: { onClick() }, label: {
            Text(text)
          })
        )
      }
    )
  }
Kotlin:
Copy code
@OptIn(ExperimentalForeignApi::class)
fun TestController(
  createUIViewController: (
    String,
    () -> Unit
  ) -> UIViewController,
): UIViewController {
  return ComposeUIViewController {
    var count by remember { mutableStateOf(0) }

    UIKitViewController(
      factory = {
        createUIViewController("Clicked $count") {
          count++
        }
      },
      modifier = Modifier
        .fillMaxSize()
    )
  }
}
It’s always 0 after clicks. Am I missing something here?
a
factory
lambda of UIKitViewController is called only 1 time during composition. To keep view controller updated you need to either pass something observable to it or explicitly mutate its properties from the
update
lambda
👍 3
d
Would it be possible for you to post a code snippet? Seems so simple but I still can't figure it out for some reason...