使用delgates
創建一個協議,在你firstviewcontroller
protocol My {
func returnFilteredImage(image: UIImage)
}
class FirstViewController: UIViewcontroller, My {
...
func returnFilteredImage(image: UIImage) {
}
}
,並在您thirdViewController創建一個屬性實現和FirstViewController委託分配到這一點。
class ThirdViewController: UIViewController {
var delegate: My?
...
override func viewDidLoad() {
super.viewDidLoad()
for vc in self.navigationController!.viewControllers{
if vc is FirstViewController {
let vc1 = vc as! FirstViewController
self.delegate = vc1
self.delegate?.returnFilteredImage(imageView.image!)
self.navigationController?.popToViewController(vc, animated: true)
}
}
}
}
2.使用本地通知 check here
可以使用本地通知
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//add observer
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.didgetImage(_:)), name: "receiveImageNotification", object: nil)
}
func didgetImage(notification: NSNotification) {
if let image = notification.userInfo?["image"] as? UIImage {
// do something with your image
}
}
}
,並從第三視圖控制器做到這一點,首先通知
let imageDataDict:[String: UIImage] = ["image": image]
NSNotificationCenter.defaultCenter().postNotificationName("receiveImageNotification", object: self, userInfo: imageDataDict)
希望這會有所幫助:)
您可以使用鍵值觀察:http://nshipster.com/key-value-observing/ – henrikstroem
您是否使用NSNotificationCenter觀察器嘗試過?如果它只涉及傳遞一個值可能這可能是有用的 –
@AnkitaShah通知是這裏的簡單方法,但我想通過代理 – ankit