草庐IT

Swift App 运行但没有按钮出现

coder 2023-09-15 原文

我写了一个 Swift 应用程序,但在运行时只显示窗口。我看不到任何按钮。

这是我的代码...我试过删除 .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!,而不是检查它是否为空。我真的无法理解你在这里想要实现的目标,但你应该撕掉所有这些压倒一切的废话,直到你能清楚地表达你的意图。

关于Swift App 运行但没有按钮出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44227356/

有关Swift App 运行但没有按钮出现的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  4. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  7. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  8. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  9. ruby-on-rails - 无法让 rspec、spork 和调试器正常运行 - 2

    GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'

  10. ruby-on-rails - before_filter 运行多个方法 - 2

    是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://

随机推荐