gpt4 book ai didi

ios - 如果 segue.identifier == "showChildFiles"当 segue 标识符看起来正确时,Swift 3 准备 segue 不通过

转载 作者:行者123 更新时间:2023-11-28 21:11:18 25 4
gpt4 key购买 nike

我正在创建一个应用程序,家长可以在其中登录并查看有关其本地托儿中心的信息。但是,为登录运行 View Controller 的代码无法正常工作。一旦用户按下登录按钮,我希望我的代码使用标识符“ShowChildFiles”执行 segue。但是在 prepare(for segue: UIStoryboardSegue, sender: Any?) 函数中,我检查了正确的 segue 标识符 (if segue.identifier == "ShowChildFiles") 但是它没有传递这个 if 语句。我在失败时打印出标识符,它返回“ShowChildFiles”。

这是代码

import Foundation
import UIKit

class MyChildLoginViewController: UIViewController {

@IBOutlet weak var usernameTextField: UITextField!

@IBOutlet weak var passwordTextField: UITextField!

@IBAction func loginButtonPressed(_ sender: Any) {

var request = URLRequest(url: URL(string: "myLogin.php")!)
request.httpMethod = "POST"
let postString = "userName=" + usernameTextField.text! + "&password=" + passwordTextField.text!
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)
print("responseString = \(responseString)")

if(responseString == "true"){
//Login successful
DispatchQueue.main.async(//Not sure why but this has to be executed asynchronously
execute: {
self.performSegue(withIdentifier: "ShowChildFiles", sender: Any?.self)
})

}else if(responseString == "false"){
//incorrect password
}else if(responseString == "incorrect username"){
//We dont recognise this username
}else{
//something went wrong
}
}
task.resume()
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyChildLoginViewController.dismissKeyboard))//Dismisses keyboard when screen tapped
view.addGestureRecognizer(tap)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.

if segue.identifier == "ShowChildFiles" ,
let destination = segue.destination as? ChildFilesTableViewController {
print("preparing for segue")
destination.passedString = self.getFamID()
}else{
print(segue.identifier!)
print("not going in the correct place")
}

}

func getFamID() -> String{

let cs: [Character] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o","p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
/*var cs = Set(chars.characters)*/
var user = [Character](usernameTextField.text!.lowercased().characters)
var famID = ""
for index in 0 ... user.count - 1 {
if(!cs.contains(user[index])){
famID = famID + String(user[index])
}
}
return famID

}
//Calls this function when the tap is recognized.
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
}

最佳答案

因为您似乎已经通过日志证明 segue 标识符就是您认为的那样,"ShowChildFiles" — 事实上,它几乎不可能是其他情况,因为那是您使用的标识符首先要执行 segue — 必须得出结论,if 条件的失败部分是第二部分,let destination = segue.destination as? ChildFilesTableViewController。显然,segue 的目的地不是 ChildFilesTableViewController。 (为什么不将 print(type(of:segue.destination)) 作为日志的一部分,然后找出答案?)

关于ios - 如果 segue.identifier == "showChildFiles"当 segue 标识符看起来正确时,Swift 3 准备 segue 不通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43083106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com