【发布时间】:2018-03-04 01:02:31
【问题描述】:
我正在尝试执行一个它不起作用的 segue。
我想要做的是发送我在我的视图控制器(主)的文本字段中拥有的数据,之后我想将它发送到一个名为 OperationsController 的视图控制器,然后将它发送到另一个视图(CreateController & ListController)所以我可以使用相同的数据并将其发送到 php 文件并获取数据以填充 ListController 中的表视图。并让 CreateController 获取电子邮件(简而言之就是数据)并根据电子邮件执行查询并插入数据库。
无论如何,我尝试将数据发送到操作到标签中并且不起作用。
这是我的代码
视图控制器:。
import UIKit
class ViewController: UIViewController {
var datas:[Usuario]?
struct Usuario : Codable {
let correo: String?
let contrasena: String?
}
@IBOutlet weak var txtError: UILabel!
@IBOutlet weak var txtCorreo: UITextField!
@IBOutlet weak var txtContra: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnLogear(_ sender: Any) {
let urlString = "http://localhost:8080/swiftdb/logear.php"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
//Implement JSON decoding and parsing
do {
//Decode retrived data with JSONDecoder and assing type of Article object
let articlesData = try JSONDecoder().decode([Usuario].self, from: data)
//Get back to the main queue
DispatchQueue.main.async {
self.datas = articlesData
let aarti = self.datas
for item in aarti! {
let correos = item.correo
let contras = item.contrasena
if(item.correo == self.txtCorreo.text && item.contrasena == self.txtContra.text){
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "OP") as! OpcionesController
self.present(nextViewController, animated:true, completion:nil)
self.performSegue(withIdentifier: "segue", sender: self)
self.txtError.text = " "
} else {
self.txtError.text = "Datos Incorrectos"
}
}
}
} catch let jsonError {
print(jsonError)
}
}.resume()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? OpcionesController{
destination.name = txtCorreo.text
}
}
}
OperationsController:。
import UIKit
class OpcionesController: UIViewController {
var name: String?
@IBOutlet weak var displayLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if let nametoDisplay = name {
displayLbl.text = name
}
}
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 let nametoDisplay = name {}删除这行代码,零意义,直接写displayLbl.text = name -
没用。还是不显示