[英]Swift viewDidLoad() in second View Controller Scene not working
I'm a beginner at swift and Xcode, so this question may sound very simple. 我是swift和Xcode的初学者,所以这个问题听起来很简单。
I have two view controllers, connected by a segue. 我有两个视图控制器,通过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. 我的第一个视图控制器场景从
viewDidLoad()
函数运行代码,然后调用.performSegue()
函数以显示第二个视图控制器场景。
But now, I want to run code in that new .swift
file. 但是现在,我想在该新的
.swift
文件中运行代码。 viewDidLoad()
doesn't seem to work, so how do I get around this problem? viewDidLoad()
似乎不起作用,那么如何解决这个问题? 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 按钮会显示,但是我在
viewDidLoad()
中编写的所有内容均无效
EDIT 2 编辑2
My code for ViewController.swift
: 我的
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
: 对于
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. 在viewWillAppear()方法内添加动画代码。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//Add animation code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.