按钮不使用其中的代码?

时间:2016-03-17 02:57:55

标签: swift button

我有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上的按钮。

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果你想编辑另一个控制器中定义的组件,你应该通知这个控制器编辑它的按钮,而不是试图从你所在的地方编辑它们,有几种方法可以做到这一点,您可以使用自定义初始化(假设您有2个控制器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()
        }
    }      
}

基本上,您将为第二个视图创建自定义初始化,并在触摸按钮时从第一个视图调用它们。