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

在Xcode中使用Swift Playgrounds添加页面跳转链接失败求助

嘿,作为Swift Playgrounds的新手,遇到Next Topic链接不显示的问题很正常,我来帮你一步步排查~

先明确两种常见的"Next Topic"场景,对应不同的解决思路

情况1:你想在自定义ViewController里添加"Next Topic"按钮但没显示

如果是这种情况,大概率是你的UI元素没正确添加到视图或者缺少布局约束,试试下面的调整:

  • 检查你的FirstScene类,确保跳转按钮已经被添加到视图上,并且设置了有效的布局约束:
    比如在FirstScene的viewDidLoad方法里补充类似代码:
    class FirstScene: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = .white // 先给背景色,方便确认视图范围
            
            let nextButton = UIButton(type: .system)
            nextButton.setTitle("Next Topic", for: .normal)
            nextButton.addTarget(self, action: #selector(navigateToNext), for: .touchUpInside)
            // 关闭自动布局,手动添加约束
            nextButton.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(nextButton)
            
            // 给按钮添加居中约束,确保它能显示在屏幕可见区域
            NSLayoutConstraint.activate([
                nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                nextButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
            ])
        }
        
        @objc func navigateToNext() {
            let nextVC = Index()
            navigationController?.pushViewController(nextVC, animated: true)
        }
    }
    
  • 另外,如果你的Index是SpriteKit场景,记得把它包装在SKView里再放到ViewController中,否则直接显示会有问题:
    class Index: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            let skView = SKView(frame: view.bounds)
            let scene = SKScene(size: skView.bounds.size)
            scene.backgroundColor = .lightGray
            skView.presentScene(scene)
            view.addSubview(skView)
        }
    }
    

情况2:你想用Playground自带的章节导航(原生Next Page)

如果是想使用Swift Playgrounds/Xcode Playground原生的页面间导航(比如左侧页面列表的跳转),那你的当前做法就不对了,应该用Playground的页面结构而非UINavigationController:

  1. 先给你的Playground添加新页面:右键你的.playground文件 → 选择Add Page,给新页面命名(比如"NextTopic")
  2. 在第一个页面的代码里,添加跳转按钮并使用PlaygroundPage的原生导航方法:
    import UIKit
    import PlaygroundSupport
    
    class FirstScene: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = .white
            
            let nextButton = UIButton(type: .system)
            nextButton.setTitle("Go to Next Topic", for: .normal)
            nextButton.addTarget(self, action: #selector(goToNextPage), for: .touchUpInside)
            nextButton.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(nextButton)
            
            NSLayoutConstraint.activate([
                nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                nextButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
            ])
        }
        
        @objc func goToNextPage() {
            // 找到目标页面并触发跳转
            guard let nextPage = PlaygroundPage.current.playground?.pages.first(where: { $0.name == "NextTopic" }) else {
                print("找不到目标页面,请检查页面名称是否正确")
                return
            }
            PlaygroundPage.current.navigateTo(page: nextPage)
        }
    }
    
    PlaygroundPage.current.liveView = UINavigationController(rootViewController: FirstScene())
    
  3. 最后在新添加的"NextTopic"页面里编写你要展示的内容即可。

先确认你要的是哪种跳转方式,然后对应调整代码,应该就能解决链接不显示的问题啦~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:26:28