【发布时间】:2017-02-28 18:31:25
【问题描述】:
我正在尝试通过标有toConsoleScreen 的segue 显示一个新屏幕。我不断收到错误:
receiver() 有 没有标识符'toConsoleScreen'的segue
我 100% 确定我的 segue 标记为 toConsolescreen,并且 segue 在 Interface Builder 中连接。
'rootViewController' 没有被触及,但我为第二页创建了一个新的UIViewController,名为Console Screen,类为ConsoleViewController。
我做错了什么?它告诉我它显然存在时找不到segue toConsoleScreen。
我正在使用:
self.performSegue(withIdentifier: "toConsoleScreen", sender: self)
来自ViewController。我尝试删除 segue 并重新创建它(使用“显示”设置),以及运行 Product -> Clean 功能。
我还应该补充一点,我正在尝试在 ViewController 内部的函数中调用 performSegue。这个函数是从一个单独的Swift 文件中调用的。我对 Swift 有点陌生,那么我怎样才能做到这一点呢?
这是我当前的 ViewController.Swift 文件:
import UIKit
class ViewController: UIViewController {
@IBOutlet var UserNameField: UITextField!
@IBOutlet var PasswordField: UITextField!
func successfulLogin(Username: String) {
// Show next view \\
self.performSegue(withIdentifier: "toConsoleScreen", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func loginButton() {
login(Username: UserNameField.text!, Password: PasswordField.text!)
}
}
还有我的 Login.swift 文件:
import Foundation
import UIKit
func login(Username: String, Password: String) {
var request = URLRequest(url: URL(string: "web address")!)
request.httpMethod = "POST"
let postString = "action=login&username=\(Username)&password=\(Password)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
if responseString! == "success" {
print("Good")
// Send to new page \\
ViewController().successfulLogin(Username: Username)
}
if responseString! == "fail" {
print("failed")
// Alert Error \\
func alertPopup(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
alertController.addAction(UIAlertAction(title: "Try Again", style: UIAlertActionStyle.default, handler: nil))
}
// Add to main Queue \\
OperationQueue.main.addOperation{
alertPopup(title: "Error", message: "Invalid Login")
}
}
}
task.resume()
}
【问题讨论】:
-
你是在
viewDidLoad打电话吗? -
谢谢。我已经重申了我的问题。
-
你提到这个函数是从一个单独的 Swift 文件中调用的。该文件是 ViewController 吗?
-
我很抱歉。我现在已经包含了我所有的代码。