`focused(_:equals)` 不起作用,但已弃用的方法是 (`focusable(_:onFocusChanged:)`)。

huangapple go评论101阅读模式
英文:

`focused(_:equals)` not working but deprecated method is (`focusable(_:onFocusChanged:)`)

问题

以下是您提供的代码的翻译部分:

这是对我有效的重点代码:

```swift
struct ContentView1: View {
    @State var text: String = "testing"
    var body: some View {
        VStack {
            Text(text)
            Text("top")
                .padding()
                .focusable(true, onFocusChange: { focused in
                    text = "top focus"
                })
            Text("bottom")
                .padding()
                .focusable(true, onFocusChange: { focused in
                    text = "bottom focus"
                })
        }
        
    }
}

然而,我得到了关于.focusable(_:onFocusChange:)行的弃用警告。因此,我自然而然地想要用非弃用的解决方案替代它。我发现这是使用@FocusStatefocused(_:equals:)。所以我实现了这个:

struct ContentView2: View {
    @FocusState var focus: Focus?
    var body: some View {
        VStack {
            Text(focus?.rawValue ?? "no focus")
            Text("top")
                .padding()
                .focused($focus, equals: .top)
            Text("bottom")
                .padding()
                .focused($focus, equals: .bottom)
        }
        
    }
}

enum Focus: String {
    case top = "top focus", bottom = "bottom focus"
}

但它根本不起作用。没有视图获得焦点。请注意,这是在 macOS 上运行的,我使用的是 13.0.1 版本。


请注意,我已经删除了HTML编码,以使翻译更清晰。如果您需要进一步的帮助,请告诉我。

<details>
<summary>英文:</summary>

Here is the focus code that works for me:

struct ContentView1: View {
@State var text: String = "testing"
var body: some View {
VStack {
Text(text)
Text("top")
.padding()
.focusable(true, onFocusChange: { focused in
text = "top focus"
})
Text("bottom")
.padding()
.focusable(true, onFocusChange: { focused in
text = "bottom focus"
})
}

}

}


However, I get a deprecation warning for  the `.focusable(_:onFocusChange:)` lines. So naturally I go to replace it with the non-deprecated solution. I come to find out that that&#39;s using `@FocusState` and `focused(_:equals)`. So I implement this:

struct ContentView2: View {
@FocusState var focus: Focus?
var body: some View {
VStack {
Text(focus?.rawValue ?? "no focus")
Text("top")
.padding()
.focused($focus, equals: .top)
Text("bottom")
.padding()
.focused($focus, equals: .bottom)
}

}

}

enum Focus: String {
case top = "top focus", bottom = "bottom focus"
}


But it doesn&#39;t work at all. No views are getting focus. Keep in mind this is on macOS and I&#39;m running 13.0.1.

</details>


# 答案1
**得分**: 1

确保每个`Text`视图都可以通过使用`.focusable()`修饰符来获得焦点。虽然某些视图,比如`TextField`,默认是可获得焦点的;但`Text`则不是:

```swift
VStack {
    Text("top")
        .focusable()
        .focused($focus, equals: .top)
    Text("bottom")
        .focusable()
        .focused($focus, equals: .bottom)
}
英文:

You want to make sure each Text views are also focusable by using the .focusable() modifier. While some views, such as TextField, are focusable by default; Text is not:

VStack {
    Text(&quot;top&quot;)
        .focusable()
        .focused($focus, equals: .top)
    Text(&quot;bottom&quot;)
        .focusable()
        .focused($focus, equals: .bottom)
}

huangapple
  • 本文由 发表于 2023年6月13日 01:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定