'How to update value in previous screen in SwiftUI?
i am navigating from one screen to another and passing data using @Binding but I am alo trying to update value back to first screen when its updating in second screen.
struct FirstView: View {
@State private var valueToPass : Int = 0
var body: some View {
VStack {
Button(action: {
self.valueToPass += 1
}) {
Text("Increase value \(self.valueToPass)")
}
}
.overlay(
SecondView(valueToGet: $valueToPass)
)
}
}
struct SecondView: View {
@Binding var valueToGet: Int
var body: some View {
VStack {
Text("Show value \(valueToGet)")
.padding(.top, 50)
}
}
}
I want to change value in SecondView without dismissing overlay need updated value that in first view.
I am not sure how should i do same in reverse.
Solution 1:[1]
A C program is divided into compilation units that are compiled independly. Your program has 2 such compilation units, namely the struct_test.c and the main.c files. If a function from one compiliation unit is to be called from the other, it must know the types of the arguments and the result type. To show this information to all compilation units involved, you typically define a function prototype in a header file and include it in all compilation units, that use or define the function.
In your case you did define the function assign in struct_test.c but you did not put a function prototype in the header. Therefore if compiling the main.c file, the compiler does know nothing about the function assign and throws an error. You should provide a function prototype in your header file after the typedef declaration of the testing struct:
testing assign(void);
Note that the error message may be misleading if you use an old compiler (or even a recent compiler without explicitly stating the C standard version). This is due to the fact, that in old C versions, you could call a function without providing a prototype. In such a case the compiler implicitly assumes an int return value. This is the reason why you probably got some error like the following. (gcc 9.3.0 on ubuntu)
main.c: In function ‘main’:
main.c:9:17: warning: implicit declaration of function ‘assign’[-Wimplicit-function-declaration]
9 | test1 = assign();
| ^~~~~~
main.c:9:17: error: incompatible types when assigning to type ‘testing’ {aka ‘union <anonymous>’} from type ‘int’
So the compiler first gives a warning, that the function was implicitly declared (which is because you did not provide a prototype in the header). And then it throws an error, becaus it cannot match the int return type from that implicitly declared function with your test1 struct.
I do not know if you also got the warning in your case, but you should really enable warnings in your compiler, as C is a language with many historical syntax and language features, that are often dangerous to use.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jakob Stark |
