• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UISegmentedControl if 语句

[复制链接]
菜鸟教程小白 发表于 2022-12-11 19:55:11 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试学习 UISegmentedControl,并想弄清楚如何让它在 if 语句中工作,以根据选择返回一个值,并根据另一个选择返回另一个值。 UISegment 只能返回两个值 - “Fixed”或“Varibale”。下面的代码不起作用,但给出了我想要的想法。这两个变量是预定义的。

  @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 中的所有代码。 @IBAction 不起作用,因为根据它们在代码中的发布位置 undefined variable 。 Storyboard 有一个 UIsegmentedControl,其值为“固定和可变”。如果用户选择固定,那么我希望它按照 UISegment 控件的 IBAction 显示。



Best Answer-推荐答案


如果您想要检查分段控件的值何时发生变化,并且您想以编程方式进行,您首先需要使用 valueChanged 常量注册一个目标操作方法。然后,编写您的 valueChanged 函数。查看 this了解更多信息。

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

如果您从 Storyboard 和 Interface Builder 中引用您的 UISegmentedControl。您还应该向它添加一个操作。 enter image description here

更新已编辑的问题

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

关于ios - UISegmentedControl if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44688512/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap