Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何为单选按钮指定值_Swift_Macos_Cocoa - Fatal编程技术网

Swift 如何为单选按钮指定值

Swift 如何为单选按钮指定值,swift,macos,cocoa,Swift,Macos,Cocoa,嗨,我想做一个美元和欧元的货币转换器 我宣布单选按钮为 无线电美元 无线电操作员 我想给单选按钮赋值 美元=0.395 radioEUR=0.447 我是可可新手 我在网上尝试了很多解决方案,但都没有成功 进口可可 类ViewController:NSViewController{ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view.

嗨,我想做一个美元和欧元的货币转换器

我宣布单选按钮为 无线电美元 无线电操作员

我想给单选按钮赋值 美元=0.395 radioEUR=0.447

我是可可新手

我在网上尝试了很多解决方案,但都没有成功

进口可可

类ViewController:NSViewController{

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


@IBOutlet weak var radioUSD: NSButton!
@IBOutlet weak var radioEUR: NSButton!
@IBOutlet weak var Label: NSTextField!
@IBOutlet weak var value: NSTextField!

var us = 0.395
var eur = 0.447

@IBAction func calc(_ sender: Any) {
    Label.doubleValue = value.doubleValue * us
}

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

如果我选择的话,我希望输出是 美元价值*0.395 我们的值是*0.447


现在看来,似乎您总是要根据您的代码乘以
us
Label.doubleValue=value.doubleValue*us
。这就是你正在经历的吗?此外,您希望输入/输出是什么?如果您打开了USD单选按钮,这是否意味着您输入的是USD,您将输出EUR,或者您正在输入EUR,希望美元输出?单选按钮没有值,它有一个状态:on或off。如果
radioUSD
处于启用状态,请使用
us
;如果
radioEUR
处于启用状态,请使用
eur
。@LucasDerraugh否当我选择USD时,我希望文本框的值乘以0.395,并显示在标签中,这样当我选择eur时,我希望它乘以0。447@Willeke你能给我解释一个方法或者给我一个指导吗因为我是新来的,谢谢advance@The-_-King_464 Willeke在说什么。您的变量
us
eur
calc
中的发送方值无关(单选按钮没有us或our的值)。如果radioUSD.state==.on{multiply by usd},则需要执行类似于
的操作,否则如果radioEUR.state==.on{multiply by eur}
则只需使用
.on
而不是
NSControl.StateValue(rawValue:1)
//
//  ViewController.swift
//  Currency Converter
//
//  Create`enter code here`d by Firas Albusaidy on 6/30/19.
//  Copyright © 2019 Firas Albusaidy. All rights reserved.
//

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


    @IBOutlet weak var radioUSD: NSButton!
    @IBOutlet weak var radioEUR: NSButton!
    @IBOutlet weak var Label: NSTextField!


    @IBOutlet weak var value: NSTextField!
    var us = 0.395
    var eur = 0.447
    @IBAction func calc(_ sender: Any) {
        if (radioUSD.state == NSControl.StateValue(rawValue: 1))
        {
            Label.doubleValue = value.doubleValue * us
            print ("Radio 1 Selected");
        }
        else
        {
            Label.doubleValue = value.doubleValue * eur
            print ("Radio 2 Selected");
        }
    }

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




}