SwiftUI之页面跳转
1.更改根控制器页面:
Button.init("返回") {
let screnDelegate: UIWindowSceneDelegate? = {
var uiScreen: UIScene?
UIApplication.shared.connectedScenes.forEach { (screen) in
uiScreen = screen
}
return (uiScreen?.delegate as? UIWindowSceneDelegate)
}()
screnDelegate?.window!?.rootViewController = UIHostingController(rootView: HomeView());
或者
UIApplication.shared.windows.first?.rootViewController = UIHostingController(rootView: HomeView())
}
2.push跳转
import SwiftUIstruct HomeView:View {var body: some View{return List{ForEach(0..<3, id: \.self) { index inif index==0{NavigationLink(destination: one.init(name: "", age: 1)){Text("\(index)")}}if index==1{NavigationLink(destination: Two()){Text("\(index)")}}}}}}
push/pop:跳转的时候带参数,控制pop返回
import SwiftUIstruct HomeView:View {@State var isShow = falsevar body: some View{//isActive是控制是否自动跳转,false不自动跳转,跳转到下一个页面后isShow就变成了trueNavigationLink(destination: one(showing:$isShow), isActive: self.$isShow) {Text("one")}}}
pop返回:mport SwiftUIstruct one: View {@Binding var showing :Boolvar body: some View {Button("点击返回") {self.showing = false//这个控制pop返回}}}
pop到根视图:在SwiftUI下实现popToRootViewController()返回根视图 - 知乎
present:(fullScreenCover)
import SwiftUIstruct HomeView:View {@State var isPresented = falsevar body: some View{Button("present"){self.isPresented = true}.fullScreenCover(isPresented: $isPresented) {print("消失")} content: {Two()}}
}
import SwiftUIstruct Two: View {@Environment(\.presentationMode) var presentationModessvar body: some View {Text("Hello,two")Button("返回"){self.presentationModess.wrappedValue.dismiss()//返回的方法}}
}
sheet:(和present类似,只需改变fullScreenCover为sheet)
import SwiftUIstruct HomeView:View {@State var isPresented = falsevar body: some View{Button("sheet"){self.isPresented = true}.sheet(isPresented: $isPresented) {//这个是页面消失后的回调print("消失")} content: {Two()}}
}
present返回
import SwiftUIstruct Two: View {
//该视图将创建一个名为presentationMode的属性,该属性附加到存储在应用程序环境中的演示模式变量中@Environment(\.presentationMode) var presentationModessvar body: some View {Text("Hello,two")Button("返回"){
//需要在其中添加wrapdValue,因为presentationMode实际上是一个绑定,因此它可以由系统自动更新——我们需要在其中进行解包以检索实际的呈现方式,以关闭视图。self.presentationModess.wrappedValue.dismiss()//返回的方法}}
}
alert:提示框,最多两个按钮
import SwiftUIstruct HomeView:View {@State var showAlert = falsevar body: some View{Button("alert"){self.showAlert = true}.alert(isPresented: $showAlert){
// Alert(title: Text("one"), message: Text("two"), dismissButton: .default(Text("ok")))return Alert(title: Text("one"), message: Text("one"), primaryButton: .cancel(Text("取消"), action: {print("取消")}), secondaryButton:.default(Text("确定"), action: {print("确定")}))}}
}
actionsheet:提示框,可以有多个按钮
import SwiftUIstruct HomeView:View {@State var isPresented = falsevar body: some View{Button("present"){self.isPresented = true}.actionSheet(isPresented: $isPresented) {ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [.default(Text("Red")) { print("Red") },.default(Text("Green")) { print("green") },.default(Text("Blue")) {print("blue") },.cancel()])}}
}
跳转到safari浏览器:必须要加上http
Link(destination: URL(string: "https://www.baidu.com")!) {Text("百度搜索")
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
