I have a simple iOS app written in Swift that chooses a random background color every time a button is pushed. There is a "Credits" button in the view that acts as a modal segue to another view. How would I go about setting the color of the second view based on whatever color the background is in the first view?
You need to pass color from first controller to second. below is the snippet.
let destinationVC:ViewControllerClass = segue.destinationViewController as! ViewControllerClass
destinationVC.color = view.backgroundColor
You can archive this like this:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Load View") {
// pass data to next view
let viewController:ViewController = segue!.destinationViewController as ViewController
viewController.view.backgroundColor = self.view.backgroundColor
}
}
You can pass some arguments to the view controller you're presenting or pushing by overriding prepareForSegue
method :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let secondViewController = segue.destinationViewController as? MySecondViewControllerClass {
secondViewController.view.backgroundColor = view.backgroundColor
}
}
You can find more infos available about prepareForSegue
on UIViewController documentation from Apple.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.