简体   繁体   中英

Swift viewDidLoad() in second View Controller Scene not working

I'm a beginner at swift and Xcode, so this question may sound very simple.

I have two view controllers, connected by a segue. My first view controller scene runs code from the viewDidLoad() function, and then calls the .performSegue() function for the second view controller scene to get displayed.

But now, I want to run code in that new .swift file. viewDidLoad() doesn't seem to work, so how do I get around this problem? Where else could I put my code, or what function should it be in?

EDIT

// Show main menu
self.performSegue(withIdentifier: "MenuSegue", sender: self)

is how I am switching between view controllers. The buttons display, but anything I write in viewDidLoad() does not work

EDIT 2

My code for ViewController.swift :

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var startScreen: UIStackView! // H2O start screen




    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        // Display start screen for 2 seconds before fading out
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: {

            // Fade out start screen
            UIView.animate(withDuration: 1, animations: {self.startScreen.alpha = 0})

            // Wait 1 second from when start screen starts to fade out
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {

                // Show main menu
                self.performSegue(withIdentifier: "MenuSegue", sender: self)

            })

        })

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

And for MenuViewController.swift :

import UIKit

class MenuViewController: UIViewController {


    @IBOutlet weak var playButton: UIButton! // Play button
    @IBOutlet weak var settingsButton: UIButton! // Settings button

    @IBOutlet weak var volumeButton: UIButton! // Volume button
    @IBOutlet weak var volumeStack: UIStackView! // Volume stack
    @IBOutlet weak var volumePercentage: UILabel! // Volume percentage




    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        print("Hello")


        // Slowly display everything
        playButton.alpha = 0
        settingsButton.alpha = 0
        volumeButton.alpha = 0
        volumeStack.alpha = 0

        // Fade out start screen
        UIView.animate(withDuration: 1, animations: {self.playButton.alpha = 1; self.settingsButton.alpha = 1; self.volumeButton.alpha = 1; self.volumeStack.alpha = 1})

        print("Hi")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }





    @IBAction func settings(_ sender: UIButton) {

        // If the settings are hidden
        if volumeButton.alpha == 0 {

            // Rotate settings button to open settings
            UIView.animate(withDuration: 0.5, animations: {sender.transform = CGAffineTransform(rotationAngle: .pi * -0.999)})

            // Show extended settings
            UIView.animate(withDuration: 0.5, animations: {self.volumeButton.alpha = 1})

        }
        else {

            // Rotate settings button back to normal
            UIView.animate(withDuration: 0.5, animations: {sender.transform = CGAffineTransform(rotationAngle: 0.0)})

            // Hide extended settings
            UIView.animate(withDuration: 0.5, animations: {self.volumeButton.alpha = 0})

        }

    }


    @IBAction func volumeSlider(_ sender: UISlider) {

        // Get slider value rounded to nearest 5
        let value = round(sender.value / 5) * 5

        // Set the value to nearest 5
        sender.value = value

        // Show volume percentage
        volumePercentage.text = "\(Int(value))%"

    }


    @IBAction func playButton(_ sender: UIButton) {

        // Hide all settings
        settingsButton.alpha = 0
        volumeButton.alpha = 0
        volumeStack.alpha = 0

        // Hide play button
        sender.alpha = 0

    }


}

Add your animation code inside viewWillAppear() method.

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
       //Add animation code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM