如果 segue.identifier == "showChildFiles" 当 segue 标识符看起来正确时,Swift 3 准备 segue 不通过
Posted
技术标签:
【中文标题】如果 segue.identifier == "showChildFiles" 当 segue 标识符看起来正确时,Swift 3 准备 segue 不通过【英文标题】:Swift 3 prepare for segue not passing if segue.identifier == "showChildFiles" when the segue identifier seems correct 【发布时间】:2017-03-29 02:15:32 【问题描述】:我正在创建一个应用程序,家长可以在其中登录并查看有关当地儿童保育中心的信息。但是,为登录运行视图控制器的代码无法正常工作。一旦用户按下登录按钮,我希望我的代码使用标识符“ShowChildFiles”执行segue。但是在 prepare(for segue: UIStoryboardSegue, sender: Any?) 函数中,我检查了正确的 segue 标识符(如果 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)
【问题讨论】:
【参考方案1】:由于您似乎已通过日志记录证明 segue 标识符就是您认为的那样,"ShowChildFiles"
— 事实上,它几乎不可能是其他情况,因为那是您用于 perform
中的 segue 的标识符第一个地方 — 必须得出结论,if
条件失败的部分是 second 部分,let destination = segue.destination as? ChildFilesTableViewController
。显然,segue 的目的地是不是 ChildFilesTableViewController。 (为什么不将print(type(of:segue.destination))
作为日志记录的一部分并找出答案?)
【讨论】:
谢谢,是的,原因是 segue.destination 不正确。我先把它转到 UINavigationController 而不是直接转到我的 ChildFilesTableViewController。以上是关于如果 segue.identifier == "showChildFiles" 当 segue 标识符看起来正确时,Swift 3 准备 segue 不通过的主要内容,如果未能解决你的问题,请参考以下文章
如何从 UICollectionView indexwise 传递数据
如果我在 Kriskowal 的 q 中多次拒绝/解决会发生啥?