Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct RadioButtonView: View {
- @State private var selectedRadioButton: Int? = nil
- let options: [String]
- var body: some View {
- VStack {
- ForEach(options.indices, id: \.self) { index in
- ContainerView(
- isSelected: Binding(
- get: { self.selectedRadioButton == index },
- set: { newValue in
- if newValue {
- self.selectedRadioButton = index
- }
- }
- ),
- label: options[index]
- )
- }
- }
- }
- }
- struct ContainerView: View {
- @Binding var isSelected: Bool
- let label: String
- var body: some View {
- HStack {
- ThirdPartyRadioButton(isSelected: $isSelected)
- Text(label)
- }
- .padding()
- .background(Color.gray.opacity(0.2))
- .cornerRadius(8)
- }
- }
- // Placeholder for the third-party radio button component
- struct ThirdPartyRadioButton: View {
- @Binding var isSelected: Bool
- var body: some View {
- Button(action: {
- self.isSelected = true
- }) {
- Image(systemName: isSelected ? "largecircle.fill.circle" : "circle")
- .foregroundColor(isSelected ? .blue : .gray)
- }
- }
- }
- struct ContentView: View {
- var body: some View {
- RadioButtonView(options: ["Option 1", "Option 2", "Option 3"])
- }
- }
- @main
- struct MyApp: App {
- var body: some Scene {
- WindowGroup {
- ContentView()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement