swift 如何获取用户正在按下按钮的窗口的ID?

k97glaaz  于 11个月前  发布在  Swift
关注(0)|答案(1)|浏览(88)

在我的macOS应用程序中,我可以打开几个带有文本字段和按钮的窗口。当用户键入一些文本并按下按钮时,我需要检索窗口的id,以便在特定于窗口的模式下处理输入。
我想有一些简单的解决方案,但我无法通过查看文档和各种帖子找到它。请理解我是Swift的新手。如果我的问题对这个网站来说太天真了,请说出来,我会弃权。

pjngdqdw

pjngdqdw1#

我找到了一个暂时的基本解决办法。我想这将被认为是初步的,但我会集中精力在完善我的应用程序的界面后。不过,如果有任何改进建议,我将不胜感激。本质上,我为每个精化模式都有一个不同的WindowGroupvar modeKey: Int标签也出现在ContentView中,并被我的按钮捕获。这些窗口可以在File -> New菜单中打开。就这些了缺点是模式的数量是固定的。感谢您的关注。
下面是三种模式的代码:

import SwiftUI

@main
struct example00App: App {
  var body: some Scene {
    WindowGroup("modeKey: 0", id: "main") {
      ContentView(modeKey: 0)
    }
    WindowGroup("modeKey: 1", id: "1") {
      ContentView(modeKey: 1)
    }
    WindowGroup("modeKey: 2", id: "2") {
      ContentView(modeKey: 2)
    }
  }
}

struct ContentView: View {
  var modeKey : Int
  @State private var outputText = ""
  var body: some View {
    VStack(alignment: .leading) { Button(action: {outputText = "\(modeKey)"}) {
      Text("modeKey").padding(EdgeInsets(top: 10, leading: 1, bottom: 10, trailing: 1)) }
      TextEditor(text: $outputText)
    }
  }
}

字符串

相关问题