在Swift中使用多个按钮会导致级联输出?

时间:2016-11-07 04:11:07

标签: swift button output cascading

很抱歉这样一个新手问题,但我没有编程经验,甚至无法想到如何搜索我的问题的答案。

背景

我正在使用Wemos D1 Mini R2控制一块Neopixel可寻址LED,这样的草图允许我通过网络发送http请求以更改模式并打开或关闭LED等。

我决定开始编写我的第一个iPhone应用程序来替换使用网页,并且第一个按钮取得了巨大的成功,可以打开或关闭LED。

当我添加更多按钮时,我遇到了麻烦。

如果按第一条消息上的第一个按钮,则按原样发送:

response = Optional(<NSHTTPURLResponse: 0x170221200> { URL: pttp://10.0.1.147/rainbow?fade=3000 } { status code: 200, headers {
    "Access-Control-Allow-Origin" = "*";
    Connection = close;
    "Content-Length" = 7;
    "Content-Type" = "text/plain";
} })

当我按下一个按钮时,我得到第一个输出和第二个输出?

response = Optional(<NSHTTPURLResponse: 0x17003de00> { URL: pttp://10.0.1.147/rainbow?fade=3000 } { status code: 200, headers {
    "Access-Control-Allow-Origin" = "*";
    Connection = close;
    "Content-Length" = 7;
    "Content-Type" = "text/plain";
} })
response = Optional(<NSHTTPURLResponse: 0x170220e80> { URL: pttp://10.0.1.147/wave?r=255&g=32&b=10&fade=5000 } { status code: 200, headers {
    "Access-Control-Allow-Origin" = "*";
    Connection = close;
    "Content-Length" = 4;
    "Content-Type" = "text/plain";
} })

以及第三个等等:

response = Optional(<NSHTTPURLResponse: 0x170222340> { URL: pttp://10.0.1.147/setleds?r=32&g=64&b=32&fade=5000 } { status code: 200, headers {
    "Access-Control-Allow-Origin" = "*";
    Connection = close;
    "Content-Length" = 7;
    "Content-Type" = "text/plain";
} })
response = Optional(<NSHTTPURLResponse: 0x170220c60> { URL: pttp://10.0.1.147/rainbow?fade=3000 } { status code: 200, headers {
    "Access-Control-Allow-Origin" = "*";
    Connection = close;
    "Content-Length" = 7;
    "Content-Type" = "text/plain";
} })
response = Optional(<NSHTTPURLResponse: 0x17403e400> { URL: pttp://10.0.1.147/wave?r=255&g=32&b=10&fade=5000 } { status code: 200, headers {
    "Access-Control-Allow-Origin" = "*";
    Connection = close;
    "Content-Length" = 4;
    "Content-Type" = "text/plain";
} })

我很确定我犯了一个错误的错误,我放置了我的代码或简单的东西,但我找不到答案。这是我的代码,非常感谢任何帮助。

//
//  ViewController.swift
//  Neopixel Controller
//
//  Created by Andrew Brierley on 6/11/16.
//  Copyright © 2016 Andrew Brierley. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    //START RAINBOW BUTTON HERE
    @IBAction func Rainbow(_ sender: UIButton) {

        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/rainbow?fade=3000"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
            { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
            print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()

    }

    // START ALL OFF BUTTON HERE
    @IBAction func AllOff(_ sender: UIButton) {

        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/ledsoff?fade=500"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
        { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()


    }

    //START WAVE BUTTON HERE
    @IBAction func Wave(_ sender: UIButton) {

        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/wave?r=255&g=32&b=10&fade=5000"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
        { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()



    }



    //START RED BUTTON HERE
    @IBAction func RED(_ sender: UIButton) {

        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/setleds?r=32&g=64&b=32&fade=5000"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
        { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()
    }




    //START GREEN BUTTON HERE
    @IBAction func GREEN(_ sender: UIButton) {

        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/setleds?r=000&g=255&b=000&fade=1000"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
        { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()

    }

    //START BLUE BUTTON HERE
    @IBAction func BLUE(_ sender: UIButton) {

        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/setleds?r=000&g=000&b=255&fade=0"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
        { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()

    }


    //START WHITE BUTTON HERE
    @IBAction func WHITE(_ sender: UIButton) {

        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/setleds?r=127&g=127&b=127&fade=10000000000"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
        { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated

    }
}

0 个答案:

没有答案