You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS16.0中NavigationLink(destination:isActive)弃用问题求助

iOS 16及以上版本中,旧的NavigationLink(isActive:)初始化方法已被弃用,需要改用NavigationStack结合.navigationDestination的新导航方案,以下是针对你代码的具体修改步骤:

1. 更新ViewModel,添加导航路径管理

在你的PhoneLoginViewModel中添加导航路径变量,并修改sendCode方法触发导航:

class PhoneLoginViewModel: ObservableObject {
    // 原有代码保持不变...
    @Published var navigationPath = NavigationPath()
    
    func sendCode() {
        // 原有发送验证码的逻辑...
        // 完成后触发导航
        navigationPath.append(())
    }
}

如果想更规范地管理路由,可以定义一个枚举:

enum AuthRoute {
    case verification
}

class PhoneLoginViewModel: ObservableObject {
    // 原有代码保持不变...
    @Published var navigationPath = NavigationPath()
    
    func sendCode() {
        // 原有发送验证码的逻辑...
        navigationPath.append(AuthRoute.verification)
    }
}

2. 修改视图结构,替换旧导航逻辑

移除原来的隐藏NavigationLink,用NavigationStack包裹根视图,并添加.navigationDestination关联目标页面:

struct PhoneLogin: View {
    @StateObject var phoneLoginData = PhoneLoginViewModel()
    @State var isSmall = UIScreen.main.bounds.height < 750
    
    var body: some View {
        // 包裹NavigationStack并绑定路径
        NavigationStack(path: $phoneLoginData.navigationPath) {
            VStack {
                VStack {
                    Text("Continue With Phone")
                        .font(.title2)
                        .fontWeight(.bold)
                        .foregroundColor(.black)
                        .padding()
                    
                    Image("phone_logo")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .padding()
                    
                    Text("You'll receive a 4-digit code \n to verify next")
                        .font(isSmall ? .none : .title2)
                        .foregroundColor(.gray)
                        .multilineTextAlignment(.center)
                        .padding()
                    
                    // Mobile Number Field . . . . .
                    
                    HStack {
                        VStack(alignment: .leading, spacing: 6) {
                            Text("Enter Your Number")
                                .font(.caption)
                                .foregroundColor(.gray)
                            
                            Text("+ \(phoneLoginData.getCountryCode()) \(phoneLoginData.phNo)")
                                .font(.title2)
                                .fontWeight(.bold)
                                .foregroundColor(.black)
                        }
                        
                        Spacer(minLength: 0)

                        // 移除原来的隐藏NavigationLink
                        
                        Button(action: phoneLoginData.sendCode, label: {
                            Text("Continue")
                                .foregroundColor(.black)
                                .padding(.vertical, 18)
                                .padding(.horizontal, 38)
                                .background(Color.yellow)
                                .cornerRadius(15)
                        })
                        .disabled(phoneLoginData.phNo.isEmpty)
                    }
                    .padding()
                    .background(Color.white)
                    .cornerRadius(20)
                    .shadow(color: Color.black.opacity(0.1), radius: 5, x: 0, y: -5)
                }
                .frame(height: UIScreen.main.bounds.height / 1.8)
                .background(Color.white)
                .cornerRadius(20)
                
                // Custom Number Pad
                CustomNumberPad(value: $phoneLoginData.phNo, isVerify: false)
            }
            .background(Color("bg").ignoresSafeArea(.all, edges: .bottom))
            // 添加导航目标关联
            .navigationDestination(for: Void.self) {
                Verification(phoneLoginData: phoneLoginData)
            }
            // 如果用枚举路由,替换成下面的代码:
            // .navigationDestination(for: AuthRoute.self) { route in
            //     switch route {
            //     case .verification:
            //         Verification(phoneLoginData: phoneLoginData)
            //     }
            // }
        }
    }
}

关键说明

  • NavigationStack是新导航体系的核心,通过绑定NavigationPath来管理导航栈的状态
  • .navigationDestination负责将路由标记(或类型)与目标视图关联,替代了旧的isActive触发方式
  • 这种方式更符合SwiftUI的状态驱动设计,也避免了旧API中隐藏视图带来的潜在问题

内容的提问来源于stack exchange,提问作者LizG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:21:01