我正在尝试设置一个视图,该视图可以显示多个模式,具体取决于所点击的按钮。
当我只添加一个时sheet,一切正常:
.sheet(isPresented: $showingModal1) { ... }
Run Code Online (Sandbox Code Playgroud)
但是,当我添加另一张纸时,只有最后一张可用。
.sheet(isPresented: $showingModal1) { ... }
.sheet(isPresented: $showingModal2) { ... }
Run Code Online (Sandbox Code Playgroud)
更新
我试图使此工作正常,但是我不确定如何为声明类型modal。我收到的错误Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements。
struct ContentView: View {
@State var modal: View?
var body: some View {
VStack {
Button(action: {
self.modal = ModalContentView1()
}) {
Text("Show Modal 1")
}
Button(action: {
self.modal = ModalContentView2()
}) {
Text("Show Modal 2")
} …Run Code Online (Sandbox Code Playgroud) 假设我List在一行中有一个和两个按钮,如何在不突出显示整个行的情况下区分哪个按钮被轻拍了?
对于此示例代码,当点击该行中的任何一个按钮时,将同时调用两个按钮的动作回调。
// a simple list with just one row
List {
// both buttons in a HStack so that they appear in a single row
HStack {
Button(action: {
print("button 1 tapped")
}) {
Text("One")
}
Button(action: {
print("button 2 tapped")
}) {
Text("Two")
}
}
}
// when tapping just once on either button:
// "button 1 tapped"
// "button 2 tapped"
Run Code Online (Sandbox Code Playgroud) 我.sheet(isPresented: self.$showModal)在我的根视图中使用来呈现一个模态。在模态中,我NavigationView用来引导用户浏览各种页面(对于用户配置文件构建器)。
在我的导航堆栈的最后一页中,我使用 @Environment(\.presentationMode) var presentationMode了一个按钮self.presentationMode.wrappedValue.dismiss()来关闭模态。但是,这只会关闭导航堆栈中的最后一页,而我只是在上一页结束。我想要做的是关闭整个导航堆栈并最终回到根视图。
向下滑动以关闭模态会产生所需的结果,但我想使用按钮以编程方式执行此操作。
这在 SwiftUI 中目前是否可行,或者这是NavigationView在模态表中使用的问题吗?
我想使用变量在视图中调用不同的操作表。这并不意味着工作。
.actionSheet(isPresented: self.$neuinitialisierung) {
ActionSheet(
title: Text("Testtitel 1"),
message: Text("Testmessage 1"),
buttons: [
.default(Text("Button 1"),
action: {
print("KLICK")
}),
.default(Text("Button 2"),
action: {
self.clouddienst_waehlen = true;
})
])
}
.actionSheet(isPresented: self.$clouddienst_waehlen) {
ActionSheet(
title: Text("Testtitel 2"),
message: Text("Testmessage 2"),
buttons: [
.default(Text("Button 1"),
action: {
print("KLICK")
}),
.default(Text("Button 2"),
action: {
self.clouddienst_waehlen = true;
})
])
}
Run Code Online (Sandbox Code Playgroud)
如果我只用一张动作表尝试它,它就会起作用。我怎样才能使用第二个?
如果我通过选择另一个项目来更改值,我会尝试将一个项目传递到工作表。即使我从未使用第一项调用工作表,工作表仍会继续显示第一项。
这是一个错误还是我做错了什么?
锻炼列表.swift
import SwiftUI
struct WorkoutList: View {
private var workouts = workoutData
@State var showWorkoutDetails = false
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 20) {
ForEach(workouts) { workoutCard in
GeometryReader { geometry in
Button(action: {self.showWorkoutDetails.toggle()}) {
WorkoutCardView(workout: workoutCard)
.rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
.sheet(isPresented: self.$showWorkoutDetails) {
WorkoutDetails(workout: workoutCard)
}
}
}
.frame(width:246, height: 360)
}
}
.padding(30)
}
}
}
private let workoutData = [ …Run Code Online (Sandbox Code Playgroud) 我在这里发布之前做了一些研究,但我无法修复它。
在注册视图中,我希望用户注册。
我创建了一个链表,当用户注册用户名时,我的程序会检查用户名是否已被占用。
如果被占用,它应该发出警报,说明用户名已被占用,因为用户点击了注册按钮。
如果用户名没有被占用,那么它应该显示一个警报,说注册成功
import SwiftUI
struct registerScreen: View {
@State var username: String = ""
@State var password: String = ""
@State private var sucessfulRegister = false
@State private var failedRegister = false
var body: some View {
VStack {
TextField()
SecureField()
Button(action: {
let userinfo = linkedList()
if (userinfo.contains(value: self.username)){
// self.failedRegister = true
self.failedRegister.toggle()
// show alert that it failed
} else {
userinfo.insert(value: user(username: self.username, password: self.password))
// show alert that it is successfull
self.sucessfulRegister.toggle() …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 .sheet 演示文稿将多个模态视图附加到 NavigationView。
我尝试将 .sheet 链接在一起只是为了发现当绑定变量更改时只能触发最后一个显示
有没有办法为多个模态做到这一点?
var body: some View {
NavigationView {
ZStack(alignment: .leading) {
MainView()
}
// present profile page
.sheet(isPresented: self.$presentation.profile){
ProfilePage()
}
// present product page
.sheet(isPresented: self.$presentProduct) {
SingleProductView()
}
//present login
.sheet(isPresented: self.$showLogin) {
LoginView(showLogin:self.$showLogin)
}
//present cart
.sheet(isPresented: self.$showCart) {
CartView()
}
// set title
.navigationBarTitle("Title", displayMode: .inline)
// set items
.navigationBarItems(leading: (
NavigationBarLeadingItems()
),trailing: (
NavigationBarTrailingItems()
)
)
Run Code Online (Sandbox Code Playgroud)