0

I know how to modify property of an object from another class in parent/child relationship mode, But, If is not in parent/child relationship i really can't make it, anyone can solve my problem?

First viewController:

class ViewController: UIViewController {

var uview:UIView = UIView()

override func viewDidLoad() {
    super.viewDidLoad()

    uview.frame = CGRectMake(55, 175, 100, 100)
    uview.backgroundColor = UIColor.greenColor()
    self.view.addSubview(uview)

    var sec = second()
    self.view.addSubview(sec.view)

}
}

Second viewController:

class second:UIViewController{

var button = UIButton()

var color = UIColor.blackColor()
override func viewDidLoad() {
    super.viewDidLoad()

    button.frame = CGRectMake(55, 55, 100, 100)
    button.backgroundColor = UIColor.redColor()
    button.addTarget(self, action: "press", forControlEvents:UIControlEvents.TouchUpInside)
    self.view.addSubview(button)

}

func press(){
    var first:ViewController = ViewController()
    first.uview.backgroundColor = UIColor.purpleColor()

}
}
Joe
  • 8,868
  • 8
  • 37
  • 59
George NG
  • 25
  • 1
  • 6
  • refer this answer http://stackoverflow.com/questions/30839152/its-possible-to-change-font-of-uilabel-from-other-viewcontroller-class It will help you – Rizwan Shaikh Jul 24 '15 at 11:17
  • In both class you can add "var secondViewController: UIViewController?" and set this property in press function after creating view "var first:ViewController = ViewController()" – lukszar Jul 24 '15 at 13:17

1 Answers1

0

You add a delegate like this question or you can simply declare the second view controller as an instance property on the first:

class ViewController: UIViewController {
    var uview:UIView = UIView()
    var sec : second!

    override func viewDidLoad() {
        super.viewDidLoad()

        uview.frame = CGRectMake(55, 175, 100, 100)
        uview.backgroundColor = UIColor.greenColor()
        self.view.addSubview(uview)

        self.sec = second()
        self.view.addSubview(sec.view)
    }
}
Community
  • 1
  • 1
Code Different
  • 90,614
  • 16
  • 144
  • 163