gpt4 book ai didi

swift - 按钮不使用其中的代码?

转载 作者:行者123 更新时间:2023-11-30 13:36:09 24 4
gpt4 key购买 nike

我有 2 个按钮,可以更改下一个 ViewController 上 6 个按钮的颜色。这是我正在使用的代码:

@IBAction func pHTargetButton(sender: UIButton) {

chemistryButton0?.setTitle("6.2", forState: UIControlState.Normal)
chemistryButton1?.setTitle("6.8", forState: UIControlState.Normal)
chemistryButton2?.setTitle("7.2", forState: UIControlState.Normal)
chemistryButton3?.setTitle("7.8", forState: UIControlState.Normal)
chemistryButton4?.setTitle("8.4", forState: UIControlState.Normal)
chemistryButton5?.setTitle("", forState: UIControlState.Normal)

chemistryButton0?.backgroundColor = UIColor(red: 212, green: 142, blue: 69)
chemistryButton1?.backgroundColor = UIColor(red: 209, green: 122, blue: 31)
chemistryButton2?.backgroundColor = UIColor(red: 224, green: 80, blue: 9)
chemistryButton3?.backgroundColor = UIColor(red: 194, green: 74, blue: 58)
chemistryButton4?.backgroundColor = UIColor(red: 208, green: 48, blue: 75)
chemistryButton5?.backgroundColor = UIColor(red: 255, green: 255, blue: 255)
chemistryButton5?.alpha = 0.0

}

@IBAction func ammoniaTargetButton(sender: UIButton) {

chemistryButton0?.setTitle("0.00", forState: UIControlState.Normal)
chemistryButton1?.setTitle("0.25", forState: UIControlState.Normal)
chemistryButton2?.setTitle("0.50", forState: UIControlState.Normal)
chemistryButton3?.setTitle("1.00", forState: UIControlState.Normal)
chemistryButton4?.setTitle("3.00", forState: UIControlState.Normal)
chemistryButton5?.setTitle("6.00", forState: UIControlState.Normal)

chemistryButton0?.backgroundColor = UIColor(red: 244, green: 235, blue: 130)
chemistryButton1?.backgroundColor = UIColor(red: 233, green: 233, blue: 156)
chemistryButton2?.backgroundColor = UIColor(red: 223, green: 238, blue: 141)
chemistryButton3?.backgroundColor = UIColor(red: 221, green: 236, blue: 210)
chemistryButton4?.backgroundColor = UIColor(red: 202, green: 227, blue: 191)
chemistryButton5?.backgroundColor = UIColor(red: 202, green: 216, blue: 173)
chemistryButton5?.alpha = 1.0

}

当我单击第一个按钮时,除了从一个 ViewControler 转移到下一个 ViewControler 之外,没有任何反应。我的另一个第二个按钮也会发生同样的情况。我想知道我是否只是在代码中遗漏了某些内容,或者是否需要以某种方式连接第二个 ViewController 上的按钮。

非常感谢任何意见或建议。

提前致谢。

最佳答案

如果您想编辑在另一个 Controller 中定义的组件,您应该通知 Controller 编辑其按钮,而不是尝试从您所在的位置编辑它们,有多种方法可以做到这一点,您可以使用自定义 init(假设您有 2 个 Controller VC1.swiftVC2.swift):

VC1.swift

import UIKit

class VC1: UIViewController {
// your buttons
@IBOutlet var greenButton: UIButton!
@IBOutlet var redButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func makeThemGreen(sender: AnyObject) {
// How you call your new view
let vc2view = VC2(color: vc2Style.Green)
self.presentViewController(vc2view, animated: true, completion: nil)
}

@IBAction func makeThemRed(sender: AnyObject) {
let vc2view = VC2(color: vc2Style.Red)
self.presentViewController(vc2view, animated: true, completion: nil)
}
}

VC2.swift

import UIKit

// you create an enum to propose different style for your buttons or other components
enum vc2Style: Int {
case Green
case Red
}

class VC2: UIViewController {
@IBOutlet var btnA: UIButton!
@IBOutlet var btnB: UIButton!
@IBOutlet var btnC: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// convenience init for making custom init
convenience init(color: vc2Style) {
self.init()
// check the style which has been "ordered" and display your components with custom style!
switch(color) {
case .Green:
self.view.backgroundColor = UIColor.greenColor()

case .Red:
self.view.backgroundColor = UIColor.redColor()
}
}
}

所以基本上,您将为第二个 View 创建自定义 init,并在触摸按钮时从第一个 View 调用它们。

关于swift - 按钮不使用其中的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com