我正在尝试通过 segue 显示一个新屏幕,标记为 toConsoleScreen
。我不断收到错误:
Receiver () has no segue with identifier 'toConsoleScreen'
我 100% 确定我的 Segue 被标记为 toConsolescreen
,并且 Segue 已在 Interface Builder
中连接。
“rootViewController”尚未被触及,但我为第二页创建了一个新的 UIViewController
,名为 Console Screen
,其中包含一个 ConsoleViewController< 类
。
我做错了什么?它告诉我,当它明显存在时,它找不到segue toConsoleScreen
。
我正在使用:
self.performSegue(withIdentifier: "toConsoleScreen", sender: self)
来自ViewController
。我尝试删除 segue 并重新创建它(使用“显示”设置),以及运行“产品”->“清理”功能。
我还应该补充一点,我正在尝试在 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()
}
最佳答案
您不应该像这样调用该方法:
ViewController().successfulLogin(Username: Username)
上面的代码将初始化一个新的 ViewController 实例。您应该使用委托(delegate)模式或完成处理程序来调用 successfulLogin
方法。
使用完成处理程序的示例:
在ViewController.swift
中:
@IBAction func loginButton() {
login(Username: UserNameField.text!, Password: PasswordField.text!) { username in
self.successfulLogin(Username: username)
}
}
在Login.swift
中:
func login(Username: String, Password: String, completion: @escaping (String) -> Void) {
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 \\
completion(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()
}
关于ios - 从错误 : "Receiver has no segue with identifier" 启动 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42499772/