[英]Swift- Pass textfield from swift file to other swift file
I am a newbie and I try to explain what my problem is: 我是新手,我尝试解释我的问题是什么:
I have a swift file its name is feedmodel.swift: 我有一个swift文件,其名称为feedmodel.swift:
import Foundation
protocol FeedmodelProtocol: class {
func itemsDownloaded(items: NSArray)
}
class Feedmodel: NSObject, URLSessionDataDelegate {
weak var delegate: FeedmodelProtocol!
func downloadItems() {
let myUrl = URL(string: "http://example.net/stock_service4.php");
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
var request = URLRequest(url:myUrl!)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "Latitudee=19.4&Longitudee=-99.1";
request.httpBody = postString.data(using: .utf8)
let task = defaultSession.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: 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 = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
self.parseJSON(data)
}
task.resume()
}
and I have a swift file its name is NeuerBeitragViewController: 我有一个快速文件,名称为NeuerBeitragViewController:
import UIKit
import CoreLocation
class NeuerBeitragViewController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var Tankstelle: UITextField!
@IBOutlet weak var Kraftstoff1: UITextField!
@IBOutlet weak var Preis1: UITextField!
@IBOutlet weak var Kraftstoff2: UITextField!
@IBOutlet weak var Preis2: UITextField!
@IBOutlet weak var Notiz: UITextField!
@IBOutlet weak var Longitude: UITextField!
@IBOutlet weak var Latitude: UITextField!
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
startLocation = nil
}
@IBAction func startWhenInUse(_ sender: Any) {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
let latestLocation: CLLocation = locations[locations.count - 1]
Latitude.text = String(format: "%.4f",
latestLocation.coordinate.latitude)
Longitude.text = String(format: "%.4f",
latestLocation.coordinate.longitude)
}
func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
print(error.localizedDescription)
}
In my NeuerBeitragViewController.Swift
I have this line:
Latitude.text = String(format: "%.4f",
latestLocation.coordinate.latitude)
Longitude.text = String(format: "%.4f",
latestLocation.coordinate.longitude)
And I want to get the Value for Latitude.text and Longitude.text in my Feedmodel.swift in this line here: 我想在此行中的Feedmodel.swift中获取Latitude.text和Longitude.text的值:
let postString = "firstName=19.4&lastName=-99.1";
So that I can do this here: 这样我就可以在这里执行此操作:
let postString = "firstName=\(Latitude.text)&lastName=\Longitude.text";
Hope you guys understood what I need and can help. 希望你们能理解我的需求并能提供帮助。
Thank You! 谢谢!
To pass your information from one view to another you have several options : 要将您的信息从一个视图传递到另一个视图,您有几种选择:
In your case, using Protocols & Deleguates is the proper option to choose. 在您的情况下,选择“协议和代理”是正确的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.