我有一個Bool firstAppear在我的SecondViewController中。播放視頻時,布爾變爲假。然後,如果我回到我的FirstViewController並返回,則視頻將再次播放。據我所知,我總是創建ViewController的另一個實例,這就是爲什麼我的邏輯是錯誤的。我需要做些什麼來實現它?firstAppear將無法正常工作,ViewController初始化每次
@IBAction func nextButtonPressed(sender: AnyObject) {
if let secondViewController = storyboard?.instantiateViewControllerWithIdentifier("answer") as? SecondViewController{
SecondViewController.id = self.id
}
self.navigationController?.pushViewController(secondViewController, animated: true)
}
並於二:
private var firstAppear = true
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if firstAppear {
do {
try playVideo()
firstAppear = false
} catch AppError.InvalidResource(let name, let type) {
debugPrint("Could not find resource \(name).\(type)")
} catch {
debugPrint("Generic error")
}
}
}
@IBAction func pop(sender: AnyObject) {
self.navigationController?.popViewControllerAnimated(true)
}
好的,但是,我應該做更多的同一屏幕的實例嗎?這是正確的方法嗎? – PoolHallJunkie
通常你不應該調用'instantiateViewControllerWithIdentifier'。你應該使用Storyboard segue。但是這也會創建多個實例。如果視圖控制器當前不在屏幕上,它們永遠不應該存在。 (有關segues,請參閱https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/StoryboardSegue.html。) –