当前位置:   article > 正文

SwiftUI之页面跳转_swiftui页面跳转

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跳转

  1. import SwiftUI
  2. struct HomeView:View {
  3.     var body: some View{
  4.         return List{
  5.                     ForEach(0..<3, id: \.self) { index in
  6.                         if index==0{
  7. NavigationLink(destination: one.init(name: "", age: 1)){
  8.                                 Text("\(index)")
  9. }
  10.                         }
  11.                         if index==1{
  12.                             NavigationLink(destination: Two()){
  13.                                 Text("\(index)")
  14.                             }
  15.                         }
  16.                        }
  17.                    }
  18.       }
  19. }

 push/pop:跳转的时候带参数,控制pop返回

  1. import SwiftUI
  2. struct HomeView:View {
  3. @State var isShow = false
  4. var body: some View{
  5. //isActive是控制是否自动跳转,false不自动跳转,跳转到下一个页面后isShow就变成了true
  6. NavigationLink(destination: one(showing:$isShow), isActive: self.$isShow) {
  7. Text("one")
  8. }
  9. }
  10. }

  1. pop返回:
  2. mport SwiftUI
  3. struct one: View {
  4. @Binding var showing :Bool
  5. var body: some View {
  6. Button("点击返回") {
  7. self.showing = false//这个控制pop返回
  8. }
  9. }
  10. }

pop到根视图:在SwiftUI下实现popToRootViewController()返回根视图 - 知乎

present:(fullScreenCover)

  1. import SwiftUI
  2. struct HomeView:View {
  3. @State var isPresented = false
  4. var body: some View{
  5. Button("present"){
  6. self.isPresented = true
  7. }.fullScreenCover(isPresented: $isPresented) {
  8. print("消失")
  9. } content: {
  10. Two()
  11. }
  12. }
  13. }

  1. import SwiftUI
  2. struct Two: View {
  3. @Environment(\.presentationMode) var presentationModess
  4. var body: some View {
  5. Text("Hello,two")
  6. Button("返回"){
  7. self.presentationModess.wrappedValue.dismiss()//返回的方法
  8. }
  9. }
  10. }

sheet:(和present类似,只需改变fullScreenCover为sheet)

  1. import SwiftUI
  2. struct HomeView:View {
  3. @State var isPresented = false
  4. var body: some View{
  5. Button("sheet"){
  6. self.isPresented = true
  7. }.sheet(isPresented: $isPresented) {//这个是页面消失后的回调
  8. print("消失")
  9. } content: {
  10. Two()
  11. }
  12. }
  13. }

   present返回

  1. import SwiftUI
  2. struct Two: View {
  3. //该视图将创建一个名为presentationMode的属性,该属性附加到存储在应用程序环境中的演示模式变量中
  4. @Environment(\.presentationMode) var presentationModess
  5. var body: some View {
  6. Text("Hello,two")
  7. Button("返回"){
  8. //需要在其中添加wrapdValue,因为presentationMode实际上是一个绑定,因此它可以由系统自动更新——我们需要在其中进行解包以检索实际的呈现方式,以关闭视图。
  9. self.presentationModess.wrappedValue.dismiss()//返回的方法
  10. }
  11. }
  12. }

alert:提示框,最多两个按钮

  1. import SwiftUI
  2. struct HomeView:View {
  3. @State var showAlert = false
  4. var body: some View{
  5. Button("alert"){
  6. self.showAlert = true
  7. }.alert(isPresented: $showAlert){
  8. // Alert(title: Text("one"), message: Text("two"), dismissButton: .default(Text("ok")))
  9. return Alert(title: Text("one"), message: Text("one"), primaryButton: .cancel(Text("取消"), action: {
  10. print("取消")
  11. }), secondaryButton:.default(Text("确定"), action: {
  12. print("确定")
  13. }))
  14. }
  15. }
  16. }

actionsheet:提示框,可以有多个按钮

  1. import SwiftUI
  2. struct HomeView:View {
  3. @State var isPresented = false
  4. var body: some View{
  5. Button("present"){
  6. self.isPresented = true
  7. }.actionSheet(isPresented: $isPresented) {
  8. ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [
  9. .default(Text("Red")) { print("Red") },
  10. .default(Text("Green")) { print("green") },
  11. .default(Text("Blue")) {print("blue") },
  12. .cancel()
  13. ])
  14. }
  15. }
  16. }

跳转到safari浏览器:必须要加上http

  1. Link(destination: URL(string: "https://www.baidu.com")!) {
  2. Text("百度搜索")
  3. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/780932
推荐阅读
相关标签
  

闽ICP备14008679号