从启动画面视图控制器切换到主导航控制器(最好是包含数据)

时间:2019-07-25 14:17:05

标签: swift uikit segue splash-screen

我正在尝试向我的应用程序添加一个动画启动屏幕,当从Web请求中获取数据时,该屏幕是可见的。问题是我似乎无法从SplashViewController过渡到我的表ViewController,因为该表嵌入在MasterNavigationController中。

我还希望能够在Splash VC和Table VC之间传输数据-我也许可以避免使用全局变量,但这对我来说似乎是一种糟糕的做法...

我已经尝试过

  • 在将所有数据都提取到后更改根VC(使用UIApplication.shared.keyWindow?.rootViewController)-这不起作用

  • 在表VC上添加子视图-看起来非常糟糕,即使隐藏导航栏并将子视图的alpha设置为1.0,该表的标题和搜索栏仍然可见。

  • 使用setViewControllers方法更改Nav VC管理的视图堆栈,从而在Splash VC和Table VC之间创建自定义序列

在最近一次尝试中我最接近了-segue正常工作(尽管我使用了全局变量而不是发送数据... prepare由于某些原因无法正常工作...)

尽管出现了初始视图,但当该表视图出现时,动画还是不可见并且有时表中没有填充...

当我注释掉“ perform segue”行时,动画会播放,以便在动画完成之前运行segue,但我不知道为什么...

代码如下:

//ANIMATED LAUNCH SCREEN
import UIKit

//TWO GLOBAL VARIABLES needed to avoid pop over segues - might be bad practice - consider changing
var visualisations: [Visualisation] = [
    .init(id:0, name: "", info: "", url_name: "", tags: "", imageURL: "", gifURL: "")
]

var filteredVisualisations = [Visualisation]()

class AnimatedLaunchScreenController: UIViewController {

    func decodeJSON() {
            let url_name = "https://raw.githubusercontent.com/VedantVarshney/VisualisationsPersonal/master/DataModel"
            guard let url = URL(string: url_name) else { fatalError("JSON URL not found") }

            URLSession.shared.dataTask(with: url) { (data, _, _) in
                guard let data = data else { fatalError("JSON data not loaded") }

                do {

                    let decodedDataModel = try JSONDecoder().decode(DataModel.self, from: data)
                    visualisations = decodedDataModel.Visualisations
                    filteredVisualisations = decodedDataModel.Visualisations

                } catch {
                    print(error)
                }

            }.resume()
        }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.isNavigationBarHidden = true

        let LoadingLogoImage = UIImage(named: "logo_white_solid_NBG")
        let background = UIColor(displayP3Red: 0.090, green: 0.238, blue: 0.439, alpha: 1.0)
        let SplashView = RevealingSplashView(iconImage: LoadingLogoImage!, iconInitialSize: CGSize(width: 60, height: 60), backgroundColor: background)

        SplashView.animationType = .heartBeat

        self.view.addSubview(SplashView)

        SplashView.startAnimation(){
            print("completed animation")
        }

        decodeJSON()

        //let navVC = storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController

        //ANIMATION TO COMPLETE BEFORE TRANSITION TO NEW VC
        SplashView.heartAttack = true


        performSegue(withIdentifier: "replaceSegue", sender: self)

        print("done")

        //UIApplication.shared.keyWindow?.rootViewController = navVC

    }
}


//REPLACE SEGUE

import UIKit

class ReplaceSegue: UIStoryboardSegue {

    override func perform() {
        source.navigationController?.setViewControllers([self.destination], animated: true)
    }
}



//TABLE VC
class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    //scope functionality not built/needed yet - potential TODO
    func filterForSearch(_ searchText: String, scope: String = "All") {
        filteredVisualisations = visualisations.filter{
            searchText.isEmpty ?
                true:
                "\($0)".lowercased().contains(searchText.lowercased())

        }

        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }

    //var filteredVisualisations = [Visualisation]()

    //initialised - actual value doesn't matter - will be changed before segue

    //IMPORTANT - need this variable to be accessible to all funcs in class but also need to give it an initial value of correct type (object Visualisation). So, visualisations CANNOT be empty here otherwise will crash.
    var selectedVisualisation = visualisations[0]

    override func viewDidLoad() {
        super.viewDidLoad()

        DispatchQueue.main.async {
                    self.tableView.reloadData()
        }

        navigationController?.isNavigationBarHidden = false

        tableView.delegate = self
        tableView.dataSource = self


        navigationController?.navigationBar.prefersLargeTitles = true
        title = "Visualisations"

//ETC ETC...
    }
}


注意: 以为我会提到我在这里使用Storyboard。 另外,我希望表VC完全替换Splash VC-因此需要自定义序列。 我用于实际SplashScreen动画的代码来自https://github.com/PiXeL16/RevealingSplashView

任何帮助将不胜感激。

0 个答案:

没有答案