Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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
Ios UISegmentedControl if语句_Ios_Swift_Uisegmentedcontrol - Fatal编程技术网

Ios UISegmentedControl if语句

Ios UISegmentedControl if语句,ios,swift,uisegmentedcontrol,Ios,Swift,Uisegmentedcontrol,我正在尝试学习UISegmentedControl,并想知道如何让它在if语句中工作,以返回一个基于选择的值和另一个基于其他选择的值。UISegment只能返回两个值-“固定”或“可变”。下面的代码不起作用,但给出了我想要的想法。这两个变量是预定义的 @IBAction func calPenalty(_ sender: UIButton) { let startDate = termStartDate.text let mortgageTerm = Double(mort

我正在尝试学习UISegmentedControl,并想知道如何让它在if语句中工作,以返回一个基于选择的值和另一个基于其他选择的值。UISegment只能返回两个值-“固定”或“可变”。下面的代码不起作用,但给出了我想要的想法。这两个变量是预定义的

  @IBAction func calPenalty(_ sender: UIButton) {

    let startDate = termStartDate.text
    let mortgageTerm = Double(mortgageTermLabel.text!)
    let discount = Double(mortgageDiscountLabel.text!)
    let mtgCashback = Double(casbackRecieved.text!)
    let mtgBalance = Double(mortgageBalance.text!)
    let rate = Double(currentRate.text!)
    //let mortgageType = mortgageType

    let lender = Global.selectedRate
    if let oneYear = Double((lender?.oneYear)!),
        let twoYear = Double((lender?.twoYear)!),
        let threeYear = Double((lender?.threeYear)!),
        let fourYear = Double((lender?.fourYear)!),
        let fiveYear = Double((lender?.fiveYear)!) {

        let maturityDate = (getMaturityDate(startdate: startDate!, termMonths: Int(mortgageTerm!)))

        let monthsToMaturity = daysBetweenDates(endDate: maturityDate)/365*12

        let comparisonTerm = (IRDComparisonTerm(monthsToMaturity: monthsToMaturity))

        let cashback = cashbackRepayment(cashbackRecieved: mtgCashback!, mtgTerm: Double(mortgageTerm!), mthsToMaturity: Double(monthsToMaturity))

        print(cashback)


        var comparisonRate: Double = 0

        switch comparisonTerm
        {
        case 12:
            comparisonRate = oneYear

        case 24:
            comparisonRate =  twoYear

        case 36:
            comparisonRate =  threeYear
        case 48:
            comparisonRate = fourYear
        case 60:
            comparisonRate = fiveYear

        default:
            comparisonRate = 0

        } // end switch statement

       print(comparisonRate)

        let IRD = IRDPenalty(IRDComparisonRate: comparisonRate, mtgBalance: mtgBalance!, mthsToMaturity: Double(monthsToMaturity), discount: discount!, currentRate: rate!)

        let threeMthsInterestPenalty = threeMonthsInterestPenalty(mtgBalance: mtgBalance!, mtgRate: rate!)

        print (IRD)
        print (threeMthsInterestPenalty)


        var penalty: Double = 0
       // var totalPenalty: Double = 0

        if IRD > threeMthsInterestPenalty {

            penalty = IRD + cashback
        }else{

            penalty = threeMthsInterestPenalty + cashback
        }



      // totalPenalty = penalty + cashback

         calculationLabel.text = String(penalty)


    }

}
// triggers result based on value selected
   @IBAction func valueChanged(_ sender: UISegmentedControl) {

    switch sender.selectedSegmentIndex {
    case 0:
        calculationLabel.text = String(penalty)

    case 1:
        calculationLabel.text = String(threeMthsInterestPenalty + cashback)

    default:
         calculationLabel.text = String(penalty)
    }


}

编辑:我还在处理这个问题。我已经更新了上面的代码,以显示IBAction中的所有代码。@iAction不起作用,因为变量未根据它们在代码中的发布位置定义。情节提要有一个带有to值的UIsegmentedControl“固定和可变。如果用户选择fixed,则我希望它按照IBAction for UISegement控件显示

如果要检查分段控件的值何时更改,并且要以编程方式执行,则首先需要使用
valueChanged
常量注册目标操作方法。然后,写下您的
值更改
函数。查看更多信息

import UIKit

class ViewController: UIViewController {

    // Reference from Storyboard or Interface Builder
    @IBOutlet weak var mortgageType: UISegmentedControl!

    // For set up a UISegmentedControl programmatically
    var segmentedControl: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func loadView() {
        super.loadView()

        // Programmatically setup
        let items = ["Cinnamon", "Clove"]
        segmentedControl = UISegmentedControl(items: items)
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.frame = CGRect(x: 120, y: 200, width: 200, height: 30)
        segmentedControl.tintColor = UIColor.black
        segmentedControl.addTarget(self, action: #selector(self.valueChanged(_:)), for: .valueChanged)
        self.view.addSubview(segmentedControl)
    }

    @IBAction func valueChanged(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            print("Hi")

        case 1:
            print("Bye")

        default:
            print("Nothing")
        }
    }
}
如果从情节提要和界面生成器中引用UISegmentedControl。您还应该向其添加操作。

更新已编辑的问题

@IBAction func calPenalty(_ sender: UIButton) {
    if mortgageType.selectedSegmentIndex == 0 {
        print("Fixed")
    } else {
        print("Variable")
    }
}

您发布的代码没有上下文。使用的所有变量是什么?你到底想做什么?而且
UISegmentedControl
不会返回任何值。您所能做的就是确定选择了哪个段。哪些变量未定义?谢谢您的回复。我在我原来的帖子中添加了更多的评论。我仍在努力寻找解决方案。如果您使用界面生成器或故事板,您不必设置
分段控件
,甚至
添加目标
,只要您知道如何从UI添加操作。根据您编辑的问题,“做点什么”将是“嗨”,而“做其他事情”将是“再见”。我不确定我需要向设置功能添加什么。这是我的控制器“@IBOutlet弱var mortgageType:UISegmentedControl!”的详细信息。请查看我编辑的答案,了解如何使用UI或以编程方式实现它的完整代码。感谢您重新编写代码。我很抱歉,但我遗漏了一件事。我试图在@iAction中使用SegmentController,发送方是一个按钮。在同一个操作中可以有两个发件人吗?