Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SwiftUI教程演示按钮错误_Swift_Xcode_Swiftui - Fatal编程技术网

SwiftUI教程演示按钮错误

SwiftUI教程演示按钮错误,swift,xcode,swiftui,Swift,Xcode,Swiftui,我开始尝试新的SwiftUI框架,该框架在2019年WWDC上发布,并在上开始了教程 现在我要通过PresentationButton将配置文件连接到主屏幕。更确切地说,我是在主页中谈论这段代码的。swift: .navigationBarItems(trailing: PresentationButton( Image(systemName: "person.crop.circle")

我开始尝试新的SwiftUI框架,该框架在2019年WWDC上发布,并在上开始了教程

现在我要通过
PresentationButton
将配置文件连接到主屏幕。更确切地说,我是在
主页中谈论这段代码的。swift

            .navigationBarItems(trailing:
                PresentationButton(
                    Image(systemName: "person.crop.circle")
                        .imageScale(.large)
                        .accessibility(label: Text("User Profile"))
                        .padding(),
                    destination: ProfileHost()
                )
            )
当我第一次单击按钮时,配置文件表看起来很好,但是当我关闭它,然后再次单击按钮时,什么也没有发生

有人知道为什么会这样吗


提前感谢

这看起来像是SwiftUI中的一个bug。 这可能与从未调用
onDisappear
有关。 您可以通过添加

.onAppear{
打印(“出现个人资料”)
}翁迪萨佩尔先生{
打印(“配置文件消失”)
}
ProfileHost
查看。
出现
应与
消失
相平衡,这样才能完成解雇

可以通过实现一个函数来解决这个问题,该函数返回一个“取决于”状态变量的
PresentationButton

@State var profilePresented:Int=0
func profileButton(profilePresented:Int)->一些视图{
返回显示按钮(
图像(系统名称:“person.crop.circle”)
.imageScale(.large)
.可访问性(标签:文本(“用户配置文件”))
.padding(),
目标:ProfileHost(),
昂蒂格尔:{
让deadlineTime=DispatchTime.now()+秒(2)
DispatchQueue.main.asyncAfter(截止日期:deadlineTime,执行时间:{
self.profilePresented+=1
})
})
}
替换

.navigationBarItems(尾部:
显示按钮(
图像(系统名称:“person.crop.circle”)
.imageScale(.large)
.可访问性(标签:文本(“用户配置文件”))
.padding(),
目标:ProfileHost()
)
)

.navigationBarItems(尾部:self.profileButton(self.profilePresented))

我强烈建议不要使用此“解决方案”,只需向Apple报告错误。

解决此问题的最简单方法是保留destination:参数,并将Image对象放在大括号中:

PresentationButton(destination: ProfileHost()) {
    Image(systemName: "person.crop.circle")
        .imageScale(.large)
        .accessibility(label: Text("User Profile"))
        .padding()
}

这是一个在Xcode 11 Beta2中解决的错误:

使用更新的API时,以下各项应起作用:

PresentationButton(destination:ProfileHost()) {
    Image(systemName: "person.crop.circle")
    .imageScale(.large)
    .accessibility(label: Text("User Profile"))
    .padding()
}

这是在Beta 3中修复的。我也有同样的问题,PresentationButton(现在的PresentationLink)在嵌入.navigationBarItems时只触发了一次

您是对的,onDisappear方法从未被调用。我认为这将是错误的根本原因。已经报告了错误,谢谢你的建议是的。我可以确认这是SwiftUI的一个bug,我们应该联系苹果公司。希望它可以在下一个betaUsing Xcode 11 Beta2中修复,我仍然得到相同的行为:即按钮将第一次显示配置文件表,但不会再次启动。我查看了发行说明,没有找到任何与
PresentationButton
相关的内容(实际上没有提到)。这段代码给了我不同类型的错误,它显示了“调用中缺少参数#1的参数,插入','”。如果我给它一个标签,它会工作,但它会显示标签文本而不是person小部件。有人有解决办法吗?这就是我正在使用的,按钮仍然只会触发一次。我认为这是一种等待时机<代码>PresentationButton(目标:MessageListView().environmentObject(userData)){Image(systemName:“list.bullet”)}我正在使用Beta 3,但这对我仍然不起作用。愿意分享你的工作方案吗?