我试图通过标有toConsoleScreen
的segue显示新屏幕。我一直收到错误:
接收者()有 no segue with identifier' toConsoleScreen'
我100%确定我的segue标记为toConsolescreen
,并且segue已连接到Interface Builder
。
' rootViewController'还没有被触及,但我为名为UIViewController
的第2页创建了一个新的Console Screen
,其类为ConsoleViewController
。
我做错了什么?它告诉我,当它明显存在时,它无法找到segue toConsoleScreen
。
我正在使用:
self.performSegue(withIdentifier: "toConsoleScreen", sender: self)
来自ViewController
。我尝试删除segue并重新创建它(使用' show'设置),以及运行产品 - >清洁功能。
我还应该补充一点,我试图在performSegue
内的函数内调用ViewController
。从单独的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()
}
答案 0 :(得分:0)
你不应该这样调用这个方法:
ViewController().successfulLogin(Username: Username)
上面的代码将初始化一个ViewController的新实例。您应该使用委托模式或完成处理程序来调用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()
}