我仍然是编写代码的新手,我的快速代码需要一些帮助。我目前正在编写自己的应用程序,以便能够通过iPhone,iMac和Apple Watch上的应用程序控制Sonoff智能wifi开关。我已经使用IFTTT小应用程序设置了Webhook,这些Webhook允许我通过将URL或POST的GET内容更改为URL来更改设备的状态。我已经编写了代码并将其连接到main.storyboard上的按钮,但是我需要找到一种方法,使代码仅在触发某个开关时才运行代码的某些部分,而不是整个代码。
当前正在发生的事情是,我也不能不打开另一个灯就不能打开我的灯,因为该应用程序要运行整个代码来打开第一个灯,而要执行此操作,第二个按钮具有被按下。
是否有解决此问题的方法,或者有更简单的方法来解决此问题?预先感谢。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var lampON: UIButton! //Floor Lamp ON
@IBOutlet weak var lampOFF: UIButton! //Floor Lamp OFF
@IBOutlet weak var ledsON: UIButton! //Desk LEDS ON
@IBOutlet weak var ledsOFF: UIButton! //Desk LEDs OFF
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a
nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
//Turns Floor Lamp ON
@IBAction func tappedButton(_ sender: Any) {
let url = URL(string:
"https://maker.ifttt.com/trigger/")
//The string url continues but I removed the rest for security purposes
let task = URLSession.shared.dataTask(with: url!) { (data,
response, error) in
if error != nil {
print(error)
}
else{
let htmlContent = NSString(data: data!, encoding:
String.Encoding.utf8.rawValue)
print(htmlContent)
}
}
task.resume()
}//End of Code That Turns Floor Lamp ON
//Turns Floor Lamp OFF
@IBAction func tappedOffButton(_ sender: Any) {
let url = URL(string: "https://maker.ifttt.com/trigger/")
//The url string continues but I removed the rest for security purposes
let task = URLSession.shared.dataTask(with: url!) { (data,
response, error) in
if error != nil {
print(error)
}
else{
let htmlContent = NSString(data: data!, encoding:
String.Encoding.utf8.rawValue)
print(htmlContent)
}
}
task.resume()
} //End of Code That Turns Floor Lamp OFF
//Turns Desk LEDs ON
@IBAction func deskLEDSON(_ sender: Any) {
let url = URL(string: "https://maker.ifttt.com/")
//The url string continues but I removed the rest for security purposes
let task = URLSession.shared.dataTask(with: url!) { (data,
response, error) in
if error != nil {
print(error)
}
else{
let htmlContent = NSString(data: data!, encoding:
String.Encoding.utf8.rawValue)
print(htmlContent)
}
}
task.resume()
} //End of Code That Turns Desk LEDs ON
// Turns Desk LEDs OFF
@IBAction func deskLEDsOFF(_ sender: Any) {
let url = URL(string: "https://maker.ifttt.com/trigger/")
//The url string continues but I removed the rest for security purposes
let task = URLSession.shared.dataTask(with: url!) { (data,
response, error) in
if error != nil {
print(error)
}
else{
let htmlContent = NSString(data: data!, encoding:
String.Encoding.utf8.rawValue)
print(htmlContent)
}
}
task.resume()
} //End of Code that Turns Desk LEDs OFF
} //End of App Code
答案 0 :(得分:0)
看起来您有四个按钮和四个IBAction,每个按钮一个。您隐藏了URL,但我假设每个函数都调用了另一个URL。从理论上讲,这应该起作用。如果触发多个动作,则可能是由于将一个按钮挂接到多个IBAction上而导致的。仔细检查故事板,确保您没有意外执行此操作。将按钮链接到新的IBAction不会自动将其与其他IBAction断开连接。