SwifUI 中的“身份"是什么意思,我们如何更改某物的“身份" [英] What does 'identity' mean in SwifUI and how do we change the 'identity' of something

查看:51
本文介绍了SwifUI 中的“身份"是什么意思,我们如何更改某物的“身份"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SwiftUI 的新手,并且在连续显示警报时遇到了问题.

I'm new to SwiftUI and I'm having problems with presenting Alerts back-to-back.

.alert(item:content:) 修饰符的描述在它的定义中有这样写:

The description of the .alert(item:content:) modifier has this written in it's definition:

/// Presents an alert.
    ///
    /// - Parameters:
    ///     - item: A `Binding` to an optional source of truth for the `Alert`.
    ///     When representing a non-nil item, the system uses `content` to
    ///     create an alert representation of the item.
    ///
    ///     If the identity changes, the system will dismiss a
    ///     currently-presented alert and replace it by a new alert.
    ///
    ///     - content: A closure returning the `Alert` to present.

    public func alert<Item>(item: Binding<Item?>, content: (Item) -> Alert) -> some View where Item : Identifiable

我对如果身份发生变化,系统将关闭当前显示的警报并用新警报替换它部分特别感兴趣.由于我希望警报连续呈现,如果我能够以某种方式更改身份",我将能够实现我想要的功能 - 这就是让系统关闭当前呈现的警报并将旧警报替换为新警报(背靠背).

I'm particularly interested in the If the identity changes, the system will dismiss a currently-presented alert and replace it by a new alert part. Since I want Alerts to be presented back-to-back, if I'm somehow able to change the 'identity', I'll be able to achieve the functionality that I want - which is having the system dismiss the currently-presented alert and replacing the old Alert with a new Alert (back-to-back).

如果有人能向我解释什么是身份"以及我如何改变某些事物的身份",我将不胜感激.

If someone can explain to me what 'identity' is and how I can change the 'identity' of something I'll be extremely grateful.

(或者,如果您知道一种更好的方式来呈现连续提醒,那也会非常有帮助.)

(Or if you know a better way to present alerts back-to-back that'll also be very very helpful.)

提前致谢!

推荐答案

在下面找到按项目使用警报的演示.以及一些关于更改身份的调查结果.

Find below demo of alert-by-item usage. And some investigation results about changing identity as documented.

当您发现通过用户交互激活的示例(通过点击行)进行实验时,警报工作正常,但如文档所述,以编程方式更改身份似乎还不稳定,但警报确实已更新.

As you find experimenting with example (by tap on row) alert activated by user interaction works fine, but changing identity programmatically, as documented, seems not stable yet, however alert is really updated.

使用 Xcode 11.4/iOS 13.4 测试

Tested with Xcode 11.4 / iOS 13.4

struct SomeItem: Identifiable { // identifiable item
    var id: Int // identity
}

struct DemoAlertOnItem: View {

    @State private var selectedItem: SomeItem? = nil

    var body: some View {
        VStack {
            ScrollView (.vertical, showsIndicators: false) {
                ForEach (0..<5) { i in

                    Text("Item \(i)").padding()
                    .onTapGesture {
                        self.selectedItem = SomeItem(id: i)

                        // below simulate change identity while alert is shown
                        DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
                            self.selectedItem = nil                    // works !!

                            // self.selectedItem?.id = 100             // crash !!
                            // self.selectedItem = SomeItem(id: 100)  // crash !!
                        }
                    }
                }
            }
        }
        .alert(item: self.$selectedItem) { item in
             Alert(title: Text("Alert"), message: Text("For item \(item.id)"), dismissButton: .default(Text("OK")))
        }
    }
}

这篇关于SwifUI 中的“身份"是什么意思,我们如何更改某物的“身份"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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