Swift3 代码错误,使用了未声明的类型
我有这个错误 我能做些什么来修复它。下面是错误代码Swift3 代码错误,使用了未声明的类型,swift3,Swift3,我有这个错误 我能做些什么来修复它。下面是错误代码 @IBAction func btnActionShowViewController(_ sender: Any) { let view1:UIViewController = self.storyboard?.instantiateViewController(withIdentifier: //error is here "StoryboardIdOfview1") as? view1 self.navigationC
@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)
}
}
我相信你投错了destinationViewController。。。。作为? 视图1可能不正确。您的代码:
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
其中,View1IViewController是在identity inspector中定义的视图控制器的自定义类,如下所示:
==编辑===
根据您的评论,问题代码行应为:
let view1:UIViewController = self.storyboard?.instantiateViewController(withIdentifier: //error is here
"StoryboardIdOfview1") as? UIViewController
您正在将某个对象强制转换为一个变量,并将返回值赋给该变量。。。这是如何工作的?我只是在网上找到了它,今天复制并粘贴了它。我不知道真正的代码。我想让这些代码做的是转到另一个名为View1的视图控制器。问题是你的演员阵容。删除as?视图1。这是不需要的,因为函数返回变量所属的对象类型。如果有什么需要的话,你可以把它转换成UIViewController。你能给我发个代码吗?我不知道。我有view1作为情节提要,我也没有两个view controller。Swift东西只有一个,只有两个情节提要这还不够Jared。它标识要转到的目标控制器,但必须正确指定其类型。view1只是一个UIViewController或自定义UIViewController。如果是后者,您必须在上面的图像中准确地指定您拥有的内容。如果不是自定义的,则不指定名称,而是指定其类类型。。。你是什么意思