【问题标题】:Initiating segue from error: "Receiver has no segue with identifier"从错误中启动 segue:“接收者没有带有标识符的 segue”
【发布时间】: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()



}

【问题讨论】:

标签: ios iphone swift


【解决方案1】:

你不应该这样调用方法:

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()



}

【讨论】:

  • 谢谢!这正是我一直在努力实现的!代码运行完美,除了一个例外......我不得不将OperationQueue.main.addOperation 添加到performSegue 调用中。我欠你一杯咖啡(或者啤酒,如果你愿意的话……)。
  • 好的,所以再次回到这个......一旦下一个视图控制器加载(ConsoleViewController),我在视图上有几个不同的功能。 “签入”按钮和“更新下一个日期”按钮。这些按钮的结果将显示一个 UIAlertView。我不断收到“...视图不在窗口层次结构中!”错误。应用程序没有崩溃,但没有 AlertView 显示...任何见解@chengsam?
  • @RyanSady 您应该就此提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多