代码错误,使用未声明的类型

时间:2017-09-07 20:02:51

标签: swift3

我有这个错误 use of undeclared type "view1" 我该怎么做才能解决它。这是错误的代码。

@IBAction func btnActionShowViewController(_ sender: Any) {

    let view1:UIViewController =  self.storyboard?.instantiateViewController(withIdentifier: //error is here
"StoryboardIdOfview1") as? view1

    self.navigationController?.pushViewController(view1, animated: true)

}

这是我的完整代码。

//
//  ViewController.swift
//  app21
//
//  Created by Jared Evan Miller on 8/14/17.
//  Copyright © 2017 Jared Evan Miller. All rights reserved.
//

import UIKit
import AVFoundation

class ViewController: UIViewController {
// sounds are showed here
let soundFilenames = ["5","8","7","4","6","1","3","2","9"]
var audioPlayers = [AVAudioPlayer]()
// stop button and play from begginning
var lastAudioPlayer = 0
// back to code
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

// set up audio players
   for sound in soundFilenames{
        do {

            let url = URL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "wav")!);
            let audioPlayer = try AVAudioPlayer(contentsOf:url)

            audioPlayers.append(audioPlayer)
        }
        catch {

            // Catch the error that is thrown
            audioPlayers.append(AVAudioPlayer())


        }
               }

  }

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



// IBAction section ( always goes in between the last and second to last bracket } (  IBAction would go here)     }




}

@IBAction func buttonTapped(_ sender: UIButton) {

// Get the audioPlayer that corresponds to the button that they tapped
// middle buttons
    let lastPlayer = audioPlayers[lastAudioPlayer]
    lastPlayer.stop();
    lastAudioPlayer = sender.tag;
    lastPlayer.currentTime = 0;
    audioPlayer = audioPlayers[sender.tag]
    audioPlayer.currentTime = 0;
    audioPlayer.play()
}
// side buttons
@IBAction func buttonTapped2(_ sender: UIButton) {
    let lastPlayer = audioPlayers[lastAudioPlayer]
    lastPlayer.stop();
    lastAudioPlayer = sender.tag;
    lastPlayer.currentTime = 0;
    audioPlayer = audioPlayers[sender.tag]
    audioPlayer.currentTime = 0;
    audioPlayer.play()
}
// This Action allows users to stop the audio
// stop audio button
@IBAction func stop(_ sender: UIButton) {
    if audioPlayer.isPlaying {
        audioPlayer.stop()
   }
}

这是我有一个按钮同时有两个动作的部分,我希望第一个播放音乐,第二个显示另一个名为view1的视图控制器。感谢。

@IBAction func btnActionPlayMusic(_ sender: Any) {


}





@IBAction func btnActionShowViewController(_ sender: Any) {

    let view1:UIViewController =  self.storyboard?.instantiateViewController(withIdentifier: //error is here
"StoryboardIdOfview1") as? view1

    self.navigationController?.pushViewController(view1, animated: true)

}





}

1 个答案:

答案 0 :(得分:0)

我相信你正在正确地转换destinationViewController ....  view1可能不正确。你的代码:

let view1:UIViewController =  self.storyboard?.instantiateViewController(withIdentifier: //error is here
"StoryboardIdOfview1") as? view1

你应该这样投射:

let view1:UIViewController =  self.storyboard?.instantiateViewController(withIdentifier: //error is here
"StoryboardIdOfview1") as! view1UIViewController

其中view1UIViewController是您在身份检查器中定义的视图控制器的自定义类,如下所示:

enter image description here

=== EDIT === 根据您的评论,问题代码行应为:

let view1:UIViewController =  self.storyboard?.instantiateViewController(withIdentifier: //error is here
"StoryboardIdOfview1") as? UIViewController