Louis
04/19/2024, 11:10 AMfunc makeUIViewController(context: Context) -> UIViewController {
return Main_iosKt.TestController(
createUIViewController: { (text, onClick) -> UIViewController in
return UIHostingController(
rootView: Button(action: { onClick() }, label: {
Text(text)
})
)
}
)
}
Kotlin:
@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?Alexander Zhirkevich
04/19/2024, 11:30 AMfactory
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
lambdaDovydas
03/09/2025, 5:55 PM