当我尝试使用 NSTimer 更改视图控制器时出错(EXC_BAD_INSTRUCTION)

Error when I try to use NSTimer to change View Controller(EXC_BAD_INSTRUCTION)

我想在动画之后更改视图控制器。动画完成后,我使用 NSTimer 来更改视图控制器。动画发生在给定的默认 ViewController 上。在这个动画 运行s 一次之后,它应该使用 prepareForSegue 函数并转到第二个视图控制器。当我 运行 它时,动画有效,但它不会改变视图控制器。

import UIKit

class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
    @IBOutlet weak var imageView: UIImageView!
    
    var timer = NSTimer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var imagesNames = ["stairs", "stairs2", "stairs3", "stairs4", "stairs5", "stairs6", "stairs7", "stairs8", "stairs9", "stairs10", "stairs11", "stairs12", "stairs13", "stairs14", "stairs15", "stairs16", "stairs17", "stairs18", "stairs19", "stairs20", "stairs21", "stairs22", "stairs23", "stairs24", "stairs25", "stairs26", "stairs27", "stairs28", "stairs29", "stairs30", "stairs31", "stairs32", "stairs33", "stairs34", "stairs35", "stairs36", "stairs37", "stairs38", "stairs39", "stairs40", "stairs41", "stairs42", "stairs43", "stairs44", "stairs45", "stairs46","stairs47", "stairs48", "stairs49", "stairs50", "stairs51","stairs52", "stairs53", "stairs54", "stairs55", "stairs56","stairs57"]
        
        var images = [UIImage]()
        
        for i in 0..<imagesNames.count{
            images.append(UIImage(named: imagesNames[i])!)
        }
        
        imageView.animationImages = images;
        imageView.animationDuration = 4;
        imageView.startAnimating()
        
        _ = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: #selector(timeToMoveOn), userInfo: nil, repeats: false)
    }
    
    func timeToMoveOn() {
        self.performSegueWithIdentifier("goToMainUI", sender: self)
    }

您的屏幕截图中的控制台输出中有线索。

您正在隐式展开 imageView 但它是 nil。这可能意味着没有图像视图连接到该插座。