SwiftUI 中的按钮处理 [英] Button handling in SwiftUI

查看:30
本文介绍了SwiftUI 中的按钮处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SwiftUI 应用程序中,我有几个按钮(以 3 为例).其中之一突出显示.

In a SwiftUI app I have a few buttons (let us say 3 as an example). One of them is highlighted.

当我点击一个未突出显示的按钮时,之前突出显示的按钮会切换为非突出显示,并且被点击的按钮变为突出显示.如果我点击已突出显示的按钮,则不会发生任何事情.

When I tap on a non-highlighted button, the previously highlighted button toggles to non-highlighted and the tapped button becomes highlighted. If I tap the already highlighted button, nothing happens.

这个场景很简单,我有一个 highlighBtn 变量,当一个按钮被点击时 highlighBtn 获取被点击按钮的值.当下一次点击碰巧关闭先前突出显示的按钮时,将使用此变量.

This scenario is simple and I have a highlighBtn variable, when a button is tapped highlighBtn takes the value of the tapped button. This variable is then used when the next tap happens to toggle off the previously highlighted button.

这个循环没问题,但问题是我第一次点击的时候.由于某些原因,事情不起作用.

This cycle is OK, but the problem is when I do the first tap. For some reasons, things don't work.

这是我如何处理 highlighBtn 变量的创建:

This is how I handle the creation of the highlighBtn variable:

class ActiveButton: ObservableObject {
    @Published var highlighBtn = Button(....)
}

@StateObject var box = ActiveButton()

这是相关代码,当点击按钮时:

Here is the relevant code, when the button is tapped:

@EnvironmentObject var box: ActiveButton
....
Button(action: {
    // Toggle off the previously highlighted button.
    box.highlighBtn.highLight = false
    .... some useful processing ....
    box.highlighBtn = self
})

我应该提供的一个细节:如果我点击突出显示的按钮开始,那么一切都会正常进行.

One detail I should give: if I tap the highlighted button to start, then all works as it should.

我尝试了各种方法来解决这个看似简单的问题,但都失败了.

I have tried various method to solve this apparently simple problem but failed.

第一种方法是尝试初始化 highlighBtn 变量.第二个是尝试模拟点击突出显示的按钮.

The first method was to try to initialize the highlighBtn variable. The second was to try to simulate a tap on the highlighted button.

我一定遗漏了一些简单的东西.欢迎任何提示.

I must be missing something simple. Any tip would be welcome.

经过进一步调查......

我创建了一个演示应用程序来暴露我的问题.由于我缺乏使用 GitHub 的练习,我花了一些时间,但现在可以使用 这里.

I have created a demo app to expose my problem. Since I lack practice using GitHub, it took me some time, but it is now available here.

为此,我在 Xcode 中创建了一个 SwiftUI 应用程序.

For that I created a SwiftUI app in Xcode.

SceneDelegate.swift 中,紧跟在这行代码之后的四行代码已经根据该应用的需要进行了定制:

In SceneDelegate.swift, the four lines of code right after this one have been customized for the needs of this app:

// Create the SwiftUI view that provides the window contents.

除此之外,我所做的一切都驻留在文件 ContentView.swift 中.

Beside that all I did resides inside the file ContentView.swift.

为任何想看的人节省一些时间;这是切入正题的方法(即我面临的问题).

To save some time to anyone who is going to take a look; here is the way to get to the point (i.e. the issue I am facing).

  1. 运行应用程序(我在 iPhone 7 上运行).您将看到七个按钮出现.其中之一(随机)将突出显示.从突出显示的按钮开始,根据需要一个接一个地点击几个按钮.您将看到该应用应该如何工作.

  1. Run the app (I did it on an iPhone 7). You will see seven buttons appear. One of them (at random) will be highlighted. Starting with the highlighted button, tap on a few buttons one after another as many times as you want. You will see how the app is supposed to work.

(关闭后)再次运行应用程序.这一次,您还将根据需要多次点击几个按钮;但首先点击未突出显示的按钮之一.此时您会看到问题出现.

(After switching it off) Run the app a second time. This time you will also tap on a few buttons one after another as many times as you want; but start by tapping one of the non-highlighted button. You will see the problem appear at this point.

推荐答案

这是您问题第一部分的解决方案:三个按钮,其中最后一个点击的按钮会以背景突出显示:

Here is a solution for the first part of your question: Three buttons where the last one tapped gets highlighted with a background:

import SwiftUI

struct ContentView: View {

    enum HighlightTag {
        case none, first, second, third
    }

    @State private var highlighted = HighlightTag.none

    var body: some View {
        VStack {
            Button("First") {
                highlighted = .first
            }.background(
                Color.accentColor.opacity(
                    highlighted == .first ? 0.2 : 0.0
                )
            )
            Button("Second") {
                highlighted = .second
            }.background(
                Color.accentColor.opacity(
                    highlighted == .second ? 0.2 : 0.0
                )
            )
            Button("Third") {
                highlighted = .third
            }.background(
                Color.accentColor.opacity(
                    highlighted == .third ? 0.2 : 0.0
                )
            )
        }
    }
}

这篇关于SwiftUI 中的按钮处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆