我目前正在使用容器视图,我想使用委托通过子视图控制器更改父视图控制器 imageView 的值,但它总是返回nil
.
import UIKit
protocol updateImage {
func userIsDone(image:UIImage)
}
class ViewController: UIViewController, updateImage{
@IBOutlet weak var imageView:UIImageView!
var image = UIImage(named: "hello.png")
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image=self.image
}
func userIsDone(image: UIImage) {
self.image=image
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "containerChild"{
let nextView = segue.destinationViewController as! ControllerChild
nextView.image=self.image
nextView.delegate=self
}
}
}
class ControllerChild:UIViewController{
var image=UIImage(named: "newhello.png")
var delegate: updateImage? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func changeImage(sender:UIButton){
if(delegate != nil){
self.delegate!.userIsDone(self.image!)
print("I am Called!")
}
}
}