GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - dougdot3/Binding-UIKIt-Swift-UI: SwiftUI - Using UIViewControllerRepresentable to pass value to a ViewController

BranchesTags
Open more actions menu

Latest commit

 

History

5 Commits
5 Commits

Folders and files

NameName
Last commit message
Last commit date
Binding UIKIt Swift UI.xcodeproj
Binding UIKIt Swift UI.xcodeproj
 
 
Binding UIKIt Swift UI
Binding UIKIt Swift UI
 
 
README.md
README.md
 
 

Repository files navigation

UIViewControllerRepresentable Test

The goal was to walk through the process of binding SwiftUI to a ViewController. This is accomplished through UIViewControllerRepresentable.

In this case, although we could have used UIViewRepresentable I was trying to test ViewControllers. In the test, I chose a UILabel to show a string value that would change when a button is pressed.

In SwiftUI:

`@State private var textHere = "Filler Value" @State private var textLabel = UILabel(frame: CGRect(x: 40.0, y: 120.0, width: 250.0, height: 100.0))

var body: some View { VStack { RepresentablePreviewView(text: $textHere, label:$textLabel) VStack{ Button(action : { print("Button Pressed") self.textHere = "Button was pressed" }, label : { Text("Show Camera Preview") }) } } }`

The Issue

The value is effectively 'bound' via the UIViewControllerRepresentable:

@Binding var text: String @Binding var label: UILabel ... func updateUIViewController(_ representableViewController: RepresentableViewController, context: UIViewControllerRepresentableContext<RepresentablePreviewView>) { representableViewController.text = self.text representableViewController.label = self.label representableViewController.updateController() }

However:

  • When I tried to bind just a STRING, two instances of the label would appear
  • Binding a UILabel in addition to the string prevents this, but the label position changes when the button is clicked.

More work to do!

About

SwiftUI - Using UIViewControllerRepresentable to pass value to a ViewController

Topics

combineswiftswift5swiftuiuikituiviewcontrollerrepresentable

Resources

Readme
Activity

Stars

1 star

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.