Search code examples
swiftmacosstoryboardalamofireibaction

Swift App runs but no buttons appear


I wrote a Swift app but only the window appears when it runs. I can't see any buttons.

Here is my code... I've tried removing the .white attribute thinking maybe it was hidden behind a layer. Nothing.

//
//  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)
    }
}


}

Solution

  • Three errors:

    First, you are overriding viewDidLoad() and defining another viewDidLoad() inside of it.

    Your code:

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

    Should just look like this:

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

    Second, you are overriding viewDidAppear but never calling super.

    Your code:

    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
    
    }
    

    Should look like this:

    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
    
    }
    

    Third, you are overriding the IPAdress didSet and then setting it again. This will cause an infinite loop. You are also comparing a textField to nil, which it will never be, because it's a NSTextField!, instead of checking whether it's empty or not. I can't really make sense of what you're trying to achieve here but you should rip all this overriding nonsense out until you can clearly formulate your intention.