gpt4 book ai didi

swift - 努力为我的 DIY 智能家居应用程序编写代码

转载 作者:行者123 更新时间:2023-11-28 07:31:19 29 4
gpt4 key购买 nike

我在编写代码方面还是个新手,需要一些有关我的 swift 代码的帮助。我目前正在编写自己的应用程序,以便能够通过我的 iPhone、iMac 和 Apple Watch 上的应用程序控制我的 sonoff 智能 wifi 开关。我已经使用 IFTTT 小程序来设置 webhooks,允许我通过获取 URL 的内容或 POST 到 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

最佳答案

看起来您有四个按钮和四个 IBAction,每个按钮一个。您隐藏了 URL,但我假设每个函数都调用了不同的 URL。从理论上讲,这应该有效。如果它触发多个 Action ,这可能是将一个按钮连接到多个 IBAction 的结果。仔细检查你的 Storyboard,你不是无意中这样做的。将按钮挂接到新的 IBAction 不会自动断开它与其他 IBAction 的连接。

关于swift - 努力为我的 DIY 智能家居应用程序编写代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54564554/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com