简体   繁体   中英

Is there an easy way for a global variable between IOS and WatchOS App?

This IOS app has the watchOS app included, the sending function works an is displayed on the watch, and the Counter counts on the IOS app, now I am wondering if there is an easy way to display the counted numbers on the watch, this is the ContentView Code.

devices

    import SwiftUI

struct ContentView: View {
    var model = ViewModelPhone()
    @State var reachable = "No"
    @State var Message = ""
    @State var GolfCount :Int = 0
    var body: some View {
        VStack{
            Text("Reachable \(reachable)")

            
            Button(action: {
                if self.model.session.isReachable{
                    self.reachable = "Yes"
                }
                else{
                    self.reachable = "No"
                }
            }) {
                Text("Update")
            }
            //mesage input
            TextField("Input your message", text: $Message)
                .padding(100)
            Button(action: {
                //message sending
                self.model.session.sendMessage(["message" : self.Message], replyHandler: nil) { (error) in
                    print(error.localizedDescription)
                }
            }) {
            Text("Send")
            } //golf Counting
            Button(action: {
                
                GolfCount += 1
                })
            {
            Text("Schläge")
            Text(String(GolfCount))
            }}}}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM