Swift高级用法:navigationLink,@Binding

1.navigatioLink

1.1 要点简明

导航链接,用来做swift View的切换


//
//  ContentView.swift
//  NavigationLink
//
//  Created by Ethan on 2022/2/3.
//

import SwiftUI
struct detail:View{
    @Environment(\.presentationMode) var presentation
    
    var body: some View{
    
    VStack{
        
        Text("详细介绍")
            .font(.title2)
                               
    }
    .navigationBarTitle("详细页面")
    .navigationBarTitleDisplayMode(.inline)
    .navigationBarBackButtonHidden(true)
    .navigationBarItems(leading: Button("<<返回"){
        self.presentation.wrappedValue.dismiss()
        
                
    })
            
    
    }
    
    
}

struct ContentView: View {
    var body: some View {
        
        NavigationView{
            
            VStack(alignment: .center, spacing: 30){
                NavigationLink("第一个画面",destination: Text("Welcome to fist view"))
                    .font(.title)
                NavigationLink("第二个画面"){
                    Text("Welcome to second view")
                                                            
                }
                .font(.title)
                NavigationLink("详细画面",destination: detail())
                    .font(.title)
                
                                                                                
            }
                        
            .navigationTitle("Main Screen")
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

1.2案例截图

在这里插入图片描述
在这里插入图片描述

2.navigationView和@Binding

2.1这一节要点

1.@Binding,按地址传递,不是值传递。
2.navigationView的常用方法:

NavigationLink(
                    destination: detail(detailActive: self.$active),
                    isActive: self.$active,
                                        
                    label: {
                        
                        Button(action: {
                            
                            self.active=true
                                                      
                        }, label: {
                            
                            Text("Main Screen")
                                .font(.title)                            
                        })

//
//  ContentView.swift
//  NavigationLink
//
//  Created by Ethan on 2022/2/3.
//

import SwiftUI
struct detail:View{
    @Environment(\.presentationMode) var presentation
    //@Binding按地址传递 deltailActive--active,这两个bool类型之间进行了双向绑定($)
    @Binding var detailActive:Bool
    
    var body: some View{
    
        VStack(spacing:40){
        
        Text("详细介绍")
            .font(.title2)
        
        Button(action: {
            
            self.detailActive=false
        }, label: {
            Text("关闭画面")
                .font(.title2)
            
            
        })
            
    }
    .navigationBarTitle("详细页面")
    .navigationBarTitleDisplayMode(.inline)
    .navigationBarBackButtonHidden(true)
    .navigationBarItems(leading: Button("<<返回"){
        self.presentation.wrappedValue.dismiss()
        
    })
    }
    
    
}

struct ContentView: View {
    
    
    @State private var  active:Bool=false
    
    
    var body: some View {
        
        
        NavigationView{
            
            VStack(alignment: .center, spacing: 30){
                NavigationLink("第一个画面",destination: Text("Welcome to fist view"))
                    .font(.title)
                NavigationLink("第二个画面"){
                    Text("Welcome to second view")
                                                            
                }
                .font(.title)
                NavigationLink("详细画面",destination: detail(detailActive: self.$active),isActive: self.$active)
                    .font(.title)
                
                NavigationLink(
                
                    destination: detail(detailActive: self.$active),
                    isActive: self.$active,
                                        
                    label: {
                        
                        Button(action: {
                            
                            self.active=true
                            
                            
                        }, label: {
                            
                            Text("Main Screen")
                                .font(.title)
                            
                        })
                        
                        
                    }

                )
                
                
            }
            
            
            
        
            
            .navigationTitle("Main Screen")
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2.2案例截图

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值