第二个View Controller Scene中的Swift viewDidLoad()不起作用

时间:2018-03-14 17:36:26

标签: ios swift xcode

我是swift和Xcode的初学者,所以这个问题可能听起来很简单。

我有两个视图控制器,由segue连接。我的第一个视图控制器场景运行来自viewDidLoad()函数的代码,然后调用.performSegue()函数以显示第二个视图控制器场景。

但是现在,我想在新的.swift文件中运行代码。 viewDidLoad()似乎不起作用,那么我该如何解决这个问题呢?我还能在哪里放置我的代码,或者它应该具有什么功能?

修改

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

是我在视图控制器之间切换的方式。按钮显示,但我在viewDidLoad()中写的任何内容都不起作用

编辑2

我的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.
    }


}

对于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

    }


}

1 个答案:

答案 0 :(得分:4)

在viewWillAppear()方法中添加动画代码。

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