Swift 应用运行但没有按钮出现

Swift App runs but no buttons appear

我写了一个 Swift 应用程序,但它运行时只出现 window。我看不到任何按钮。

这是我的代码...我试过删除 .white 属性,认为它可能隐藏在一层后面。没有。

//
//  ViewController.swift
//  BraviaRemote
//
//  Created by Ed Gilroy on 7/2/17.
//  Copyright © 2017 Edward Williams. All rights reserved.
//

import Cocoa
import Alamofire

class ViewController: NSViewController, NSTextFieldDelegate {

@IBAction func MenuButton(_ sender: NSButtonCell) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAABgAw==")
}
@IBAction func ReturnButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAgAAAJcAAAAjAw==")
}
@IBAction func InfoButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAA6Aw==")
}
@IBAction func GuideButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAgAAAKQAAABbAw==")
}
@IBAction func SelectButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAABlAw==")
}
@IBAction func ChnUpButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAAQAw==")
}
@IBAction func ChnDownButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAARAw==")
}
@IBAction func VolUpButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAASAw==")
}
@IBAction func VolDownButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAATAw==")
}
@IBAction func LeftButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAA0Aw==")
}
@IBAction func RightButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAAzAw==")
}
@IBAction func UpButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAB0Aw==")
}
@IBAction func DownButton(_ sender: NSButton) {
    triggerRemoteControl(irccc: "AAAAAQAAAAEAAAB1Aw==")
}
@IBAction func OnOffButton(_ sender: NSSegmentedControl){

}

@IBOutlet weak var IPField: NSTextField!

var IPAddress: String? {
    didSet {
        if IPField != nil { IPAddress = "http://\(IPAddress!)/sony/IRCC?" }
        else {IPAddress = "http://192.168.2.7/sony/IRCC?"}
        if let ip = IPAddress { print (ip) } //Unwraps optional

    }
}
override func controlTextDidChange(_ obj: Notification) {
    if let txtField = obj.object as? NSTextField {
        if txtField.tag == 0 {
            //Validation (for later)
            IPAddress = txtField.stringValue
        }
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    func viewDidLoad() {
        super.viewDidLoad()

    }
}

override func viewDidAppear() {
    // Window Properties, including solid colour, lack of resize, movable by background.

    view.window?.titlebarAppearsTransparent = true
    view.window?.backgroundColor = NSColor.white
    view.window?.styleMask.remove(.resizable)
    view.window?.isMovableByWindowBackground = true

}

override var representedObject: Any? {
    didSet {
        // Update the view, if already loaded.
    }
}

struct SOAPEncoding: ParameterEncoding {
    let service: String
    let action: String
    let IRCCC: String

    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        var urlRequest = try urlRequest.asURLRequest()

        guard parameters != nil else { return urlRequest }

        if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
            urlRequest.setValue("text/xml", forHTTPHeaderField: "Content-Type")
        }

        let soapBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:\(action) xmlns:u=\"\(service)\"><IRCCCode>\(IRCCC)</IRCCCode></u:X_SendIRCC></s:Body></s:Envelope>"

        urlRequest.httpBody = soapBody.data(using: String.Encoding.utf8)

        return urlRequest
    }
}


func triggerRemoteControl(irccc: String) {
    Alamofire.request(IPAddress!,
                      method: .post,
                      parameters: ["parameter" : "value"],
                      encoding: SOAPEncoding(service: "urn:schemas-sony-com:service:IRCC:1",
                                             action: "X_SendIRCC", IRCCC: irccc)).responseString { response in
                                                print(response)
    }
}


}

三个错误:

首先,您要覆盖 viewDidLoad() 并在其中定义另一个 viewDidLoad()

您的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    func viewDidLoad() {
        super.viewDidLoad()

    }
}

应该看起来像这样:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

其次,您重写了 viewDidAppear 但从未调用 super.

您的代码:

override func viewDidAppear() {
    // Window Properties, including solid colour, lack of resize, movable by background.

    view.window?.titlebarAppearsTransparent = true
    view.window?.backgroundColor = NSColor.white
    view.window?.styleMask.remove(.resizable)
    view.window?.isMovableByWindowBackground = true

}

应该是这样的:

override func viewDidAppear() {
    super.viewDidAppear()
    // Window Properties, including solid colour, lack of resize, movable by background.
    view.window?.titlebarAppearsTransparent = true
    view.window?.backgroundColor = NSColor.white
    view.window?.styleMask.remove(.resizable)
    view.window?.isMovableByWindowBackground = true

}

第三,您正在覆盖 IPAdress didSet 然后重新设置它。这将导致无限循环。您还将 textField 与 nil 进行比较,但它永远不会是 nil,因为它是 NSTextField!,而不是检查它是否为空。我真的无法理解你在这里想要实现的目标,但你应该撕掉所有这些压倒一切的废话,直到你能清楚地表达你的意图。