Ios 将渐变颜色从一个视图控制器传递到另一个视图控制器
如何将collectionview单元格背景的渐变色传递给自定义视图的其他viewcontroller?如果要更改背景渐变色的“其他”viewcontroller是用于保存UICollectionView的,请使用下面的代码。。。如果是任何其他ViewController,则需要获取要更改的ViewController实例Ios 将渐变颜色从一个视图控制器传递到另一个视图控制器,ios,objective-c,iphone,swift,Ios,Objective C,Iphone,Swift,如何将collectionview单元格背景的渐变色传递给自定义视图的其他viewcontroller?如果要更改背景渐变色的“其他”viewcontroller是用于保存UICollectionView的,请使用下面的代码。。。如果是任何其他ViewController,则需要获取要更改的ViewController实例 class MyCollectionViewCell: UICollectionViewCell { var delegate: MyCollectionViewC
class MyCollectionViewCell: UICollectionViewCell {
var delegate: MyCollectionViewCellDelegate?
func funcThatChangesColorInOtherViewColtroller() {
let firstGradientColor = UIColor.red
let secondGradientColor = UIColor.blue
delegate?.viewControllerShouldUpdateBackgroundToColors(firstColor: firstGradientColor, secondColor: secondGradientColor)
}
}
protocol MyCollectionViewCellDelegate {
func viewControllerShouldUpdateBackgroundToColors(firstColor: UIColor, secondColor: UIColor)
}
class OtherViewController: UICollectionViewDataSource, MyCollectionViewCellDelegate {
...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReuseIdentifier", for: indexPath) as! MyCollectionViewCell
cell.delegate = self
return cell
}
func viewControllerShouldUpdateBackgroundToColors(firstColor: UIColor, secondColor: UIColor) {
//Set your VCs Background Gradient to the Colors in
}
}
您可以在SecondViewController中创建一个初始值设定器,以便在显示之前将所需的颜色传递给它
init(color: UIColor) {
super.init(nibName: nil, bundle: nil)
self.color = color
}
然后,当您实例化第二个ViewController并在其内部使用颜色时。另一个视图控制器是保存CollectionViewCell的CollectionView的ViewController吗?或者是其他VC?是要传递颜色的父视图控制器?您需要更具体地说明您正在尝试执行的操作。包括一些类名会有帮助。使用通知中心