我想在集合视图控制器中按下按钮时,在View Controller中更改UIView
的颜色。
在我的UIViewController
我有一个函数,当从集合视图控制器调用此函数时,该函数会更改颜色,但该函数会打印出来,但颜色不会更改。
查看控制器
static let sharedInstance = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("settingPageId") as! SettingsViewController
@IBOutlet weak var TopView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let colorsViewController = storyboard?.instantiateViewControllerWithIdentifier("colorsViewCollection")
self.addChildViewController(colorsViewController!)
colorCollectionView.addSubview((colorsViewController!.view)!)
}
func colorHasBeenSelected(){
TopView.backgroundColor = UIColor.blackColor() // color is not changed if called from collection view
print("colorHasBeenSelected") // it prints ok
}
集合视图控制器
let settingPage = SettingsViewController.sharedInstance
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
_ = settingPage.view // without it TopView returns nil
}
@IBAction func colorAction(sender: UIButton) {
settingPage.colorHasBeenSelected()
}
如何让UIView
内的UIViewController
更改UICollectionView
的背景颜色?