在swift中查看控制器

时间:2014-07-31 02:25:03

标签: swift interface-builder

我无法弄清楚如何使用第二个视图控制器来更改或添加/删除第一个视图控制器中的内容

我的第二个视图控制器中有一个变量,可以让我在第一个视图控制器中访问所有变量

var players = ViewController()
addOne3.addTarget(self, action: "player:", forControlEvents:     UIControlEvents.TouchUpInside)
view.addSubview(changeButton)

func player(sender: UIButton){

    players.addOne.backgroundColor = UIColor.greenColor()

}

我有一个功能,使用第二个视图控制器中的一个按钮来更改第一个视图控制器中另一个按钮的颜色,但没有任何更改为我想要更改的按钮

1 个答案:

答案 0 :(得分:0)

尝试在secondVC中创建协议,并在FirstVC中添加该委托。并在FirstVC中添加所需的功能或值。当您单击SecondVC中的按钮时,由于协议效果,函数值将更改。然后你可以做变色的事情。

import UIKit

protocol CustomProtocol
{
    func getValueFromButton( isitTrue : Bool)
}


class SecondViewController: UIViewController {

    var delegate : CustomProtocol?

    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 buttonsf(sender: UIButton)
    {
        delegate?.getValueFromButton(true)
    }
}

导入UIKit

class FirstViewController: UIViewController, CustomProtocol {


    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.
    }

    func getValueFromButton( isitTrue : Bool)
    {
        if isitTrue
        {
            // do this
        }

        else
        {
            // do that
        }
    }
    @IBAction func buttonsf(sender: UIButton)
    {
        var SecondVCObject : SecondViewCOntroller =  SecondViewCOntroller()
        SecondVCObject.delegate = self
    }
}