UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI Xcode 12.5 iOS 14 How to pass a string value from one view to another

Forums > SwiftUI

In my StoryCardView I have a String value I get from observing a view model.


Here is the view model

import Foundation
import Combine

class StoryViewModel: ObservableObject, Identifiable {
    private let storyRepository = StoryRepository()
    @Published var story: Story

    private var cancellables: Set<AnyCancellable> = []

    var id = ""

    init(story: Story) {
        self.story = story

        // set up binding for story between the stories id and the viewmodels
        //id then store object in cancellables so it can be canceled later
        $story
            .compactMap { $0.id }
            .assign(to: \.id, on: self)
            .store(in: &cancellables)
    }

    func remove() {
        storyRepository.remove(story)
    }

    func editStory(story: Story) {
        storyRepository.updateStory(story)
    }

}

In StoryCardView

It comes in like this storyViewModel.story

If you print storyViewModel.story on a button action in view it returns this

Story(id: “”, bodyText: “”, userId: “123”, etc)

I want to pass userId to another view

If I print storyViewModel.story.userId it prints “123”

I want to pass userId value in a sheet that renders StoryAuthorProfileView()


Thanks for having a look

Here is a link to post I wrote about this app and it has a repo if anybody wants to have a look: http://bit.ly/hacking-mvvm

3      

That probably needs a bit more explanation. If you are passing the storyViewModel into the view as a binding, then changing the values should propogate back from the view. If it is passed into another view then that view will have the new value.

3      

Sorry I did not write a better question, it was late and brain was a bit broken.

I ended up just adding a property on the StoryAuthorProfileView let storyUserID: String then just passing the userId

StoryAuthorProfileView(storyUserID: storyViewModel.story.userId ?? "")

Here is a link to post I wrote about this app and it has a repo if anybody wants to have a look: http://bit.ly/hacking-mvvm

Any feedback would be greatly appreciated.

Cheers, Mike

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.