我知道如何在父/子关系模式下从另一个类修改对象的属性,但是,如果没有父/子关系,我真的做不到,谁能解决我的问题?
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)
}
}第二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()
}
}发布于 2015-07-24 14:42:29
您可以添加像this question这样的委托,或者只需将第二个视图控制器声明为第一个视图的实例属性:
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)
}
}https://stackoverflow.com/questions/31608249
复制相似问题