我正在尝试使用ViewController
在secondViewController
和override func prepare(for segue: UIStoryboardSegue, sender: Any?)
之间传递数据。
我正在使用控制特斯拉汽车的第三方库。我想要的是,当ViewController
的第一个开关打开时,功能startCharging
将被执行。我收到此错误:
由于数据格式不正确,因此无法读取。
我假设它没有在ViewController之间传递数据。
此代码应在第一个开关打开时打印成功发送的命令。
迅速:
let vehicle = ""
let accessToken = ""
let password = ""
let email = ""
class SecondViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
UserName.delegate = self
Password.delegate = self
}
@IBAction func Enter(_ sender: Any) {
TextView.text = "Username: \(UserName.text!)\nPassword:\ (Password.text!)"
let email = (UserName.text!)
let password = (Password.text!)
teslaAPI.getAccessToken(email: email, password: password) {
(httpResponse, dataOrNil, errorOrNil) in
guard let accessToken = dataOrNil?.accessToken else {
return
}
// Set the accessToken for use with future requests
self.teslaAPI.setAccessToken(accessToken)
// Get vehicles
self.teslaAPI.getVehicles {
(httpResponse, dataOrNil, errorOrNil) in
guard let vehicle = dataOrNil?.vehicles.first else {
return
}
self.teslaAPI.wake(vehicle, completion: {
(res, _, err) in
guard res else {
return
}
})
}
}
self.performSegue(withIdentifier: "nextPage", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! ViewController
vc.finalVehicle = vehicle
var vc2 = segue.destination as! ViewController
vc2.finalAccessToken = accessToken
var vc3 = segue.destination as! ViewController
vc3.finalEmail = email
var vc4 = segue.destination as! ViewController
vc4.finalPassword = password
}
}
//This is from the other ViewController where I want the data to be
//passed.
class ViewController: UIViewController {
var finalVehicle = ""
var finalAccessToken = ""
var finalEmail = ""
var finalPassword = ""
@IBAction func chargeOffPeakSwitch(_ sender: UISwitch) {
if (sender.isOn == true)
DispatchQueue.main.asyncAfter(deadline: .now() + 30.0) {
// Change `2.0` to the desired number of seconds.
// Code you want to be delayed
//send command to unlock doors
// let command = Command.unlockDoors
let command = Command.flashLights
self.teslaAPI.send(command, to: self.finalVehicle) {
response in
if response.result {
print("Command sent successfully!")
}
}
}
}
}
答案 0 :(得分:-1)
请在git上分享您的代码,以获取更多帮助。但我希望它能起作用
class SecondViewController: UIViewController, UITextFieldDelegate {
var vehicle = ""
var accessToken = ""
var password = ""
var email = ""
override func viewDidLoad() {
super.viewDidLoad()
UserName.delegate = self
Password.delegate = self
}
@IBAction func Enter(_ sender: Any) {
TextView.text = "Username: \(UserName.text!)\nPassword:\ (Password.text!)"
let email = (UserName.text!)
let password = (Password.text!)
teslaAPI.getAccessToken(email: email, password: password) { [unowned self]
(httpResponse, dataOrNil, errorOrNil) in
guard let accessToken = dataOrNil?.accessToken else {
return
}
self.accessToken = accessToken
// Set the accessToken for use with future requests
self.teslaAPI.setAccessToken(accessToken)
// Get vehicles
self.getVehicle()
}
}
func getVehicle() {
self.teslaAPI.getVehicles { [unowned self]
(httpResponse, dataOrNil, errorOrNil) in
guard let vehicle = dataOrNil?.vehicles.first else {
return
}
self.vehicle = vehicle
self.performSegue(withIdentifier: "nextPage", sender: self)
self.invokeWake(vehicle: vehicle)
}
}
func invokeWake(vehicle:String) {
self.teslaAPI.wake(vehicle, completion: {
(res, _, err) in
guard res else {
return
}
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var vc = segue.destination as! ViewController
vc.finalVehicle = vehicle
vc.finalAccessToken = accessToken
vc.finalEmail = email
vc.finalPassword = password
}
}
//This is from the other ViewController where I want the data to be
//passed.
class ViewController: UIViewController {
var finalVehicle = ""
var finalAccessToken = ""
var finalEmail = ""
var finalPassword = ""
@IBAction func chargeOffPeakSwitch(_ sender: UISwitch) {
if (sender.isOn == true)
DispatchQueue.main.asyncAfter(deadline: .now() + 30.0) {
// Change `2.0` to the desired number of seconds.
// Code you want to be delayed
//send command to unlock doors
// let command = Command.unlockDoors
let command = Command.flashLights
self.teslaAPI.send(command, to: self.finalVehicle) {
response in
if response.result {
print("Command sent successfully!")
}
}
}
}
}